Sitemap

4 steps of Test-Driven Development (TDD)

1 min readOct 28, 2024

--

The way I think you should do test driven development is:

  1. Think about what behavior you want to introduce
  2. Find, or invent, an API where you can express that behavior
  3. Write tests, on that API, that clearly shows that behavior
  4. Implement the behavior

When deciding on API. Ask yourself what future refactorings can be done with your test being kept unchanged. If your test is likely to break when code is refactored, I would not consider it a good API, perhaps they should be written against another API?

The API can be anywhere in the application.

  • If you have a layered application, it might be on those layers.
  • If you have complex algorithm, it might be class level on a state-less method.
  • If you have a microservice, maby write tests on its REST-API.

A feature is typically covered with a combination of tests on different API:s.

Write the tests you need, not more and not less.

You should try to avoid mocking frameworks (like Mockito in Java, Jest mocks in Javascript, or similar …). I think you are likely testing implementation, not behavior, if your test has that kind of knowledge. I’m not saying you should never use these tools, just be very careful and aware of what you are doing. And again: “Find, or invent, an API where you can express that behavior”.

--

--