How TDD process works
TDD process
Automated testing
Automated testing provides a solution to the manual testing process. Instead of filling out that form one more time and hitting submit to see if the client-side validations trigger as expected, we can instruct software to perform this test for us.
Test-Driven JavaScript Development by Johansen Christian
The advantages are:
- run tests in numerous browsers with a single effort;
- rerun tests at any later stage;
- schedule tests run;
Unit test
A unit test is a piece of code that tests a piece of production code. A unit test sets application objects in a known state and then it runs the code to test, finally it compares the result to the expected outcome.
Unit test are subject to isolation guidelines:
- no test should ever depend on antoher test;
- tests should be able to run simultaneously and in any order;
- to maintain islation, sometimes is necessary to mock or stub test dependencies;
- a test should exercises only one unit, but it may do using one or more assertions;
Setup(test fixtures)
Setup, or test fixture, is a piece of code that runs before the execution of an unit test. It sets the state of the application in a “known state”.
Mocking
Most software you develop is made up of a variety of classes and components, mock objects mimic the behavior of real components.
“Mock” has become a generic term for the various types of mocks.
Here’s an article written by Martin Fowler that explain the difference between mock, stubs, fake, spy and dummy.
Assertions
Assertions are the main part of an Unit test, an assertion is a predicate that checks the state of the application. When an assertion fails, the test is stopped and the system notify the failure.
Unit test example
The following javascript code shows a simple set of unit tests:
Benefits of Unit tests
Testing your application takes time and the manual testing process is highly inefficient. There are a lot of benefits by using unit tests: regression testing, refactoring and cross-browser testing.
TDD process
Test-driven development is a programming technique that moves unit tests to the front row. In test-driven development tests are written as specification before writing production code: we must describe the problem we are trying to solve. In other words, TDD requires us to think about the results before providing the solution.
TDD steps
- Write the unit test for the function you’re going to implement, the unit test should be short and focus on a single behavior of a function. Tests describe the object you are implementing, and it should not be necessary to change them unless the object itself changes;
- Watch the test fail without writing the passing code allows us to confirm our theories about the current state of our code;
- Make the test pass by providing the simplest solution that could possibly work;
- Refactor to remove duplication. At the end, we need to review the work and make necessary adjustments to remove duplication and improve design. There is only one rule to obey during this step: tests should stay green;
Benefits of TDD
The strongest benefits of TDD is that it produces code that works and forces us to think about our code before writing it.
The TDD process describes specialized components in isolation and that honors the single responsibility principle.
Conclusion
Test-driven development is a technique designed to help produce clean code we can feel more confident in, and it will very likely reduce stress levels as well help you enjoy coding a lot more.
For more information about TDD in Javascript: TDD using Mocha and Karma