The other day, I was thinking about the way I write unit tests, and how my approach differs depending on when I write the tests. What I came to realise, is that writing the test after the fact makes me biased – in fact, it changes my whole incentive for writing the test. The code is done; now all I want is to bag another passing test to put up on my digital trophy shelf (okay, a slightly artistic exaggeration there :p). Famous philosopher of science, Sir Karl Popper, said it best in his paper Science as Falsification: “It is easy to obtain confirmations, or verifications, for nearly every theory - if we look for confirmations.”
One of the tenets of TDD is that when writing a test, it should initially fail. If you follow this rule, then instead of a green light being the “proof” that the test passes, the proof (I say proof; I mean confidence) becomes the act of making the test pass. This follows the scientific concept of Falsifiability, which aforementioned philosopher Popper came up with. In the same paper quoted above, he also writes that “every genuine test of a theory is an attempt to falsify it, or to refute it. Testability is falsifiability (…) Confirming evidence should not count except when it is the result of a genuine test of the theory; and this means that it can be presented as a serious but unsuccessful attempt to falsify the theory.”
Food for thought when you write your next test, anyway :)