Random data in Unit Tests?

There’s a compromise. Your coworker is actually onto something, but I think he’s doing it wrong. I’m not sure that totally random testing is very useful, but it’s certainly not invalid.

A program (or unit) specification is a hypothesis that there exists some program that meets it. The program itself is then evidence of that hypothesis. What unit testing ought to be is an attempt to provide counter-evidence to refute that the program works according to the spec.

Now, you can write the unit tests by hand, but it really is a mechanical task. It can be automated. All you have to do is write the spec, and a machine can generate lots and lots of unit tests that try to break your code.

I don’t know what language you’re using, but see here:

Java
http://functionaljava.org/

Scala (or Java)
http://github.com/rickynils/scalacheck

Haskell
http://www.cs.chalmers.se/~rjmh/QuickCheck/

.NET:
http://blogs.msdn.com/dsyme/archive/2008/08/09/fscheck-0-2.aspx

These tools will take your well-formed spec as input and automatically generate as many unit tests as you want, with automatically generated data. They use “shrinking” strategies (which you can tweak) to find the simplest possible test case to break your code and to make sure it covers the edge cases well.

Happy testing!

Leave a Comment