2009-02-20

Test Data Builders

Nat Pryce has a series of articles on Test Data Builders. I read those articles a couple years ago, but I didn't really grok Builders until I started using them last year. For me, enlightenment happened when I realized there are three categories of objects that you need to build in tests: the system under test, collaborators, and data objects.

Without Builders, you can build data objects using mocks (very painful) or you generate a lot of creation code. This creation code is located in the test method, or it gets extracted to helper methods, a base class, or helper classes. The creation code tends to be costly to maintain, and difficult to reuse.

With Builders in my toolkit, I use mocks to build collaborators, and I use Builders to build the data objects. The readability of my tests is much improved.

It's been a long journey for me to achieve readable maintainable tests. If you're comfortable with mocking, but still not happy with your tests, try out Test Data Builders.