DEV Community

Cover image for How to start with test driven development (TDD)
Exan Fabry
Exan Fabry

Posted on

How to start with test driven development (TDD)

What is TDD
TDD is short for test driven development. It is a way of writing code where you start to write your tests first and then write your code so your tests fail. If you start to write your tests they are going to fail at first but eventually you are going to write your code so you make sure those tests pass.

Why should I use TDD
Test driven development is a great way to make sure your code works how you want to and you won't get surprised with any errors. So if you are able to write good tests it is a safety net for you to write your code better. But if your tests are poorly written it can lead to bad code.

Advantages of TDD

  1. Quality code: Explain how TDD enforces writing only the necessary code and reduces the chance of introducing bugs.
  2. Faster debugging: Illustrate this by showing how tests act as documentation, helping developers quickly pinpoint issues.
  3. Easier to refactor: Provide an example where a safety net of tests allows developers to restructure code confidently.

Disadvantages of TDD

  1. More time consuming: You have to write a test for everything you do so it can be time consuming.
  2. Not great for tight deadlines: Since it is pretty time consuming it is very difficult for tight deadlines.
  3. Test maintenance: As your project grows you need to make new tests for new functions and change tests for features that are changing. Poorly written tests can also add frustration to the project.

The TDD cycle

  1. Write a failing test
  2. Write code to make your test pass
  3. Improve your code quality

Top comments (0)