Unit-testing – Why not write all tests at once when doing TDD

development-processtddunit testing

The Red – Green – Refactor cycle for TDD is well established and accepted. We write one failing unit test and make it pass as simply as possible. What are the benefits to this approach over writing many failing unit tests for a class and make them all pass in one go.

The test suite still protects you against writing incorrect code or making mistakes in the refactoring stage, so what's the harm? Sometimes it's easier to write all the tests for a class (or module) first as a form of 'brain dump' to quickly write down all the expected behavior in one go.

Best Answer

Test driven design is about getting your API right, not the code.

The benefit of writing the simplest failing tests first is that you get your API (which it essentially is you are designing on the fly) as simple as possible. Up front.

Any future uses (which the next tests you write are) will go from the initial simple design, instead of a suboptimal design coping with more complex cases.

Related Topic