Preventing Brittle Tests (and Production Code)

Have you written unit tests only later to find they slow you down as implementation changes cause a lot of test breakage? You think tests are not living up to the promises you’ve heard. So you toss the tests and go back to business as usual (Debug Later Programming).

Hey, not so fast. Maybe it’s not test in general, but your tests and your code. The first project I used Test-Driven Development on, in 1999/2000, we ran into this problem. We were ready to give up. But I wanted the tests to work and looked for what was wrong. In our excitement we kept copying, pasting and tweaking the test cases. They were an ugly (in hind-site) mass of duplication. Small changes made for ripple effects through the tests. But I could see, it was our fault.

Someone on Quora asked me “How do you make unit tests less brittle”. Here is a more complete answer based on having written my own bad tests and seen many learners of TDD and unit testing go the wrong direction with their designs and tests.
Continue reading