TDD is not (only) about testing

Test-Driven Development is rather a design tool than a testing tool. Believe me (or not), but developers are not great at testing. They are too focused on getting the functionality done. Testers are always going to find bugs, even if there is 100% code coverage.

What are the main benefits of TDD?

  1. Thinking first about tests forces to think of what needs to be done.
  2. Tests encourage to keep the focus on getting the right things done. And no more than needed (YAGNI).
  3. Continuous testing ensures that nothing has been unwittingly broken.
  4. Tests are validating the design. If the design is bad, then it harder to write tests.
  5. Tests are becoming also API documentation by example.
  6. TDD makes can make experimenting faster as the code is always testable.

What is, in my opinion, the most common error when adopting TDD? NOT FOCUSING enough on the REFACTOR phase. Tips:

  1. Never omit the refactoring phase.
  2. Refactor the test code and pay attention to its design. Especially to its coupling to the implementation details. Avoid over usage of mocking, use patterns like Object Mother, Test Data Builder.
  3. Follow Kent Beck’s Simple Design Rules:
    1. Passes the tests
    2. Reveals intention
    3. No duplication
    4. Fewest elements
  4. Always strive for High Cohesion and Low Coupling
  5. Do not fear to remove tests if they are not valuable. The tests should always help you.
  6. If the problem is hard and you have no idea how to write a test. Make a Spike and then go back to TDD.
  7. Practice TDD together with Pair Programming. There is a change that one of you may be more focused on writing good test cases and propose refactoring. The code should be at least readable for both of you.

Test-Driven Development is a technique that drives development. Tests are just a tool to make the development more effective. Unfortunately, it does not guarantee that you write the right tests, which would prevent bugs. But at least they guide the design of all the cases that you are aware of.

Leave a comment