Parameterized Unit Tests

posted on 2005-08-21 at 17:13:45 by Joel Ross

This is older, but Microsoft has a research paper about Parameterized unit tests. It's an interesting concept. Let's say you have a method that has a few extremes that need to be tested - let's try a simple example of multiplication. You would want to test something multiplied by 0. You'd want to test an overflow situation. Then, you'd want to test multiplying by a single negative number. Then maybe two negative numbers.

You get the idea. I listed out 4 tests above, but there could be many more. If you were to write unit tests now, you'd write 4 separate tests that all do the same thing - multiply two numbers together and check the result against a known result.

That's repetition. What if you could have a unit test that took three parameters - two numbers to multiply by, and a result. Then you feed the engine an input file - XML or something like that - and have it automatically run all cases of your unit test - and all you did was write one test.

I found this via Alan Dean, over at dotNetJunkies, and he seems to think it's a good idea, and that Nunit should look at implementing it. I wonder if they will.

Categories: Development