JUnit paramaterized tests vs. theories
By Jacob Childress on Mar 03, 2010
So what about JUnit Theories? As I understand it, Theories are functionally similar to parameterized tests, but are expressively richer. A Theory expresses the tester's understanding of how a piece of code is expected to work, moving the focus away from input/output sets.
The differences between the two are admittedly subtle. A couple differences that stand out to me are:
- Parameterized test classes may be more difficult to read, and
- Theories better express the tester's intent. Parameterized tests require a reader to infer the relationship between inputs and outputs.
At the same time, parameterized tests can often be written more quickly, as the tester's only challenge is to list a set of proper inputs and outputs; the tester does not need to abstract the relationship between the inputs and outputs into a Theory. A Theory needs to be true for all test data, and this may be tricky to express in some situations.