Categories
Development

Setting up testdata with test data builders

Doing TDD has become a way of life for me. One the one hand tests are the security net that lets me refactor my code, but is also a restricting force when it comes to changing code. Experience has showed that when I refactor, I also break a lot of tests that’s not relevant for the thing that has been changed. And the reason for this is usually set up of test data that has been scattered through my tests.

It’s not that setting up test data is that difficult, but unless you are structured and focus on not duplicating setup you will get into a mess. I found this interesting article at DZone describing a technique called Test Data Builders. Interesting idea I need to check out.

3 replies on “Setting up testdata with test data builders”

Anders,
one day I found out that a colleague of mine had used a so called “fixture”, which is nothing more than the Test Data Builder you’re talking about. I must say I was jealous that I didn’t think of it. It’s really useful. Especially when data structures become more complex, the “insert statement in test”-way of working is more pain than gain.
I’m curious of your finding when you’ve looked into it.

I know about the fixture (called object mother sometimes too I think) approach and I try using that. But it can lead to an explosion in methods because of all the different states needed in tests. The data builders would be a way to avoid creating a new method in the fixture for every possible state. Having said that, I think that as in everything else a good balance between fixtures and data builders for flexibility, is the way to go.

Leave a Reply

Your email address will not be published. Required fields are marked *