Test-driven development, you need to pass multiple tests.

Test-driven development (TDD) is a development process that occurs in very short cycles. All the requirements are expressed as test cases and the software is changed until those test cases are passed. The goal of TDD is to write clean code that works. TDD follows a cycle.

  1. Tests are added.
  2. You see how the tests fail with the current code.
  3. Code is written with the purpose of passing the tests.
  4. The tests are ran.
  5. The code is refactored.
Test Driven Development Process Cycle

Image source

After the last step, you go back to step one and repeat the whole process again, but with a different test. If a test fails, you need to go back to step three to make the necessary changes. The final refactor stage is really important, in step three you just want the tests to pass, even if the code isn’t pretty. In the last step, you are allowed to change the code as long as its functionality stays the same, the code is brought to a professional level.

TDD comes with various benefits, such as:

  • Before adding anything to the code, a test has to be passed. This means that our code will always be functional.
  • Before adding a new functionality to your code, you need to write a test. Tests should have an expected outcome in order to know if you passed it or not. Thanks to this, every new functionality will have a clear outcome.
  • Every test must be completed by itself, without relying on other tests. You need to think about how the different parts of the code are connected, leading to a more modular project.

TDD, while in theory, should work with every language, it will work better when using object-oriented languages. TDD relies on tests that small chunks of code have to pass, object-oriented languages are good at dividing code into small parts and having each of these parts be independent. Being able to divide the code into classes and each class has to pass a test will make the whole process easier.

TDD will enable you to reduce future bugs in your project significantly. Ensuring that every part of the code works perfectly is a powerful asset for TDD. Also, since you are trying to make every test independent, another test shouldn’t break past tests. TDD is a powerful tool that I haven’t used that much personally, but I can see the value in it. I will probably end up using in the future given how convenient it is.

Leave a comment

Design a site like this with WordPress.com
Get started