Test Driven Development
Many software engineers will tell you a feature, an application or even a device that has not gone through rigorous testing has far-reaching implications. This brilliant article published by Eric Elliot attributes failure to do TDD thoroughly as one of the causes of the Boeing 737 Max crashes( Must read btw). Its like when we go to the hospital for treatment, doctors carry out tests before prescribing drugs for us to take making sure they are doing the right thing.
In this article I will try to explain what TDD is , its advantages and disadvantages, the TDD cycle, what constitutes a good test? and lastly the types of tests. Enjoy…..
So what is Test Driven Development? Test driven development is the process of writing tests for your production code before you actually write the production code. That function supposed to perform a certain task in your application must be tested to ensure its maintainable and robust.
Advantages of TDD
- Developers are known for their problem solving skills so TDD comes in to help in the thought process.
- TDD helps to improve communication among the development team.
- TDD improves the structure of our production code.
- Developers wont be worried to make changes to their code for fear of code breaks.
Disadvantages of TDD
- TDD takes a long time than just writing code without tests.
- There is a likelihood of writing bad tests.
Then we have the TDD cycle called Red, Green, Refactor, a framework used by developers to do the following;
- Red -Write a failing test.
- Green -Write production code that is just enough to make the test pass.
- Refactor -And lastly, refactor the code you wrote.
How does one tell they have written a good test? “TDD the RITE Way” by Eric Elliot explains how one should approach writing a good test. Lets try and go though them together;
- Our tests should properly define our code so that developers on your team don’t have a hard time understanding its true intentions.
- Tests should be separate to avoid affecting each other negatively.
- Remember when I mentioned how tests should be rigorous enough? Your tests should satisfy all possible scenarios.
- Details or information to reproduce our tests should be available for anyone accessing our tests. Just like a readme file in our GitHub repo that explains what your project is about, the packages to install, etc.
Types of tests
The three main types of tests include;
- Unit tests
- Test individual pieces of functionality making sure each unit of the software code performs as expected.
2. Integration tests
- Make sure the individual pieces of our code work together for instance the client, API and server are communicating seamlessly.
3. E2E tests
- The user wants application to work as expected, doesn’t care about the nitty gritty. For instance, when I press the button to add an item to a cart on an ecommerce site I expect to see the item selected.
We have gone through the following;
- What TDD is ?
- advantages and disadvantages of TDD.
- The TDD cycle.
- And lastly, the three main types of tests.
We shall get our hands dirty in the next series by writing unit tests. Stay tuned… 😊