When Test Driven Development Goes Wrong

When you feel TDD is difficult when you first started TDD, that might sign that there are improvements to make in the design.

There are 5 common anti-patterns.

  1. The Liar
    Passes all tests with no useful assertions, which could be caused by chasing test coverage.
    => Write the test first, which fails with a purpose.
  2. Excessive Setup
    Test and Code are highly coupled. Hard to maintain.
    => Improve Abstraction and Separation of Concerns. Write Test first.
  3. The Giant
    Many lines of code and many assertions which caused by writing the code first before writing testing. This isn’t easy to understand.
    => Decomposed into multiple test cases.
  4. The Mockery
    Use so many mocks, which is similar to the excessive setup.
    => Review design of code. The same solution as #2.
  5. The inspector
    Violate encapsulation to make assertions, which can be caused by poor dependency injection.
    => Never compromise encapsulation for testing. Design for testability.

In summary, to practice TDD correctly, write a test first, which will help design the code testable.


reference: GOTO 2021 • When Test Driven Development Goes Wrong • Dave Farley