Nerd For Tech
Published in

Nerd For Tech

Basics of Test-Driven-Development

Photo by Angelina Litvin on Unsplash

I would like to give you a little resume about TDD, to understand this methodology in a few words.

Test-Driven-Development or TDD is about writing tests before development. It is a culture that implies develop tests for each functionality, protecting software processes, from failures during and after its completion.

Benefits

  • Any change is not small: Any change is not small: Any change, for smaller, can look, can generate an error in the application, the test shield of that.
  • Trusty when refactorizing: Refactor sometimes implies we make hard changes in the code, by the way, always exists the afraid for an application fails. A good set of tests protect us from more critical functionalities.
  • QA team limited: Have a good set of tests that runs automatically can bring us a good optimization for our resources, which we can use for the development of tests about complex functionalities.

Step by Step

To develop an example of this methodology, we’ll get the job of a wood sculptor.

  • First, the test: Before making any change over the sculpture is necessary to measure and plan each modification over the piece in the blank. By the way, it is possible to do an image in his mind about what will be the piece.
  • Carving the good: Therefore, the sculptor proceeds to cave the piece.
  • Polishing: Once the sculptor has done the modification, he needs to polish the piece and be more precise with each change.
  • Repeating previous steps: This methodology constitutes a process of evolution; for that reason, the repetition is necessary for the development of all the projects.

Software development can take many specifications before building a product. With TDD methodology, we can guarantee each documented step, and each stage of the process protected.

TDD brings too to developers the capacity to start with a clear goal and write code that complies directly with a specification.

I will show you an example with javascript (for those that doesn’t know about javascript, this example can be understood reading the steps).

TDD methodology with Javascript

Then we’re going to develop a small functionality called multiplication, which is part of a calculator. So, keep in mind the step by step of a test did with TDD methodology.

  1. Test first.
  2. Test throws an error.
  3. Make tests done developing functionality.
  4. Improve functionality.
  5. Repeat until you get good development.

Example:

  1. We will develop the multiplication functionality of a calculator. To start with our example, we’re going to create a test that validates multiplication functionality.
function multiplyTest() { 
var result = calculator.multiply(3, 3);

if (result === 9) {
console.log('Test Passed');
} else {
console.log('Test Failed');
}
};

multiplyTest();

The test calls a function of multiplication that still to be defined. Then it validates the result of the function will be expected and, according to this result, determine if the test is ok or not.

2. As a result, the test throws an error, that will be visible in the web browser console, as shown in the following image:

This error appears because the calculator application calls a method “multiply,” that we haven’t created.

3. In TDD, the focus consists of adding a more simple change for a test to pass. There isn’t necessary to develop all the multiplication logic, for now. So, ¿What is a more simple change that we can do for the test pass to return the value expected?

function multiply() { 
return 9;
};

4. The refactorization step should eliminate the return value confiding the multiplication function that we added as a solution to pass the test.

function multiply(amount1, amount2) { 
return amount1 * amount2;
};

Now, we are going to refresh the navigator to fix the tests. Will it pass as it did before?

Excelent!

Final code:

function multiply(amount1, amount2) { 
return amount1 * amount2;
};

function multiplyTest() {
var result = calculator.multiply(3, 3);

if (result === 9) {
console.log('Test Passed');
} else {
console.log('Test Failed');
}
}

multiplyTest();

Whit this exercise I have finalized the post about TDD, I hope you have learned the basics concepts of this methodology, which will help us to have a better and more trusted code.

Originally written on cunigarro.com/blog/basics-of-tdd

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store