Wednesday, 18 April 2007

As little Unit Testing as possible

When you first start learning TDD. It's all about creating unit tests. However, very quickly you learn its actually all about making as few unit tests as possible.

One of the traps people can fall into when doing TDD is testing lots and lots and lots of stuff. This means when you change your design you break lots of tests.

Ideally what you want is tests that test your intention. Tests that show what the code has to achieve. It doesn't matter too much how the code achieves it, and you don't really want to test how it achieves it. That way you can vary how it is implemented without breaking lots of tests. ie, Encapsulation.

Of course there are going to be plenty of things that may break tests as they slice through a layer of encapsulation.

However, if you test things pretending that the thing will magically do what you want without worrying about how, then it can be a heck of a lot easier to get good tests. When you test with a particular implementation in mind, you often get very concrete tests that fall over as soon as you try changing things.

No comments: