Unit Tests: DRY vs. Predictability?

Good question. I've heard before "unit tests can be wet, but not soaking wet..." For maintainability of the test, focus (or predictability) is key. The larger your team is, the more important I think that should be to you.

Another thing to consider is that if there is specific test helper code, that can become an API in itself, so it may not be a bad idea if everyone knows how to utilize it. My rule of thumb is I will remove duplication if I can do it with an IDE in an automated refactoring and I can give it a good name. A suggestion... check out the Test Data Builder pattern writeup by Nat Pryce about a more maintainable/extensible approach.

Thanks, that Test Data Builder pattern is ingenious! – Zecrates Oct 28 '09 at 8:28 +1: Don't obsess about DRY unit tests, that defeats the purpose. Tests must be super clear (to be trustworthy) and do not have to be efficient at all.

Any decent unit test library allows inheritance, so you can easily put some common code in a superclass. – S. Lott Oct 28 '09 at 15:39.

While DRY is applicable to production code it isn't always applicable to unit tests. You really want each test to be independent of each other and often that means repeating yourself. On the other hand, I do find it useful to group certain things into helper methods that are used by all tests as long as it doesn't couple the tests together then it should be fine.

One place I usually reduce duplication is by using test data builders to construct objects that exist in a particular state in my test. My rule of thumb is to keep my tests as small and as readable as possible. If using DRY can help achieve that then I use it.

If not, then I don't. :-) Hope that helps. I'm not a world expert at unit testing so I could be very wrong.

:-).

Another thing to think about - often when you are removing duplication in tests it's telling you that something is production code or design that is waiting for a legit reason (a use in the production codebase) to move it into production code. I can't remember exactly where I got that ah hah moment from... but I think it was related to TDD as if you mean it.

While DRY is applicable to production code it isn't always applicable to unit tests. You really want each test to be independent of each other and often that means repeating yourself. On the other hand, I do find it useful to group certain things into helper methods that are used by all tests as long as it doesn't couple the tests together then it should be fine.

One place I usually reduce duplication is by using test data builders to construct objects that exist in a particular state in my test.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions