I am not really sure how many unit tests I have written in my life, but on my current project I have written more then a 1000 over that last few months. One of the things I learned a long time ago is that code should only be written when it needs to because you usually need to time to do more important things, like working with customers, designing your product, architectural planning, and most importantly being with your family. One practice that can really speed things along is to write a common, reusable, late-bound, test rig. Think about it... How many times have you written a property test to see if you are setting a String value correctly, a Boolean flag, Enum, Date, etc... Honestly, how many times do you really need to write that code?
Click below to see some simple examples of reusable test rigs for thigs like string, boolean, enum, and object test rigs
samplegenerictestrigs.htm (20.75 kb)
When you are designing your test rigs keep the following in mind
- Keep things as generic and reusable as possible
- Consider using a late-bound approach which can provide a lot of flexibility
- Reflect, Reflect, Reflect... Reflection technologies can really help you write code that can adapt to the objects being passed to the rigs.
Once you have your test rigs you will find that for common testing tasks, your tests will become a lot shorter and easier to write.
'''A test for UserAgent()
Public Sub UserAgentTest()
Dim target As NewMyClass
GenericTestRigs.GenericStringPropertyTest(target, "UserAgent", "123", "", True)
As a side note, for those of you implementing CSLA-like architectures this will also allow your to encapsulate richer testing threshholds like management of overall object state, dirty flags, etc...