Difference between JUnit Theories and Parameterized Tests

JavaJunit4Parameterized

Java Problem Overview


What is the difference between a Theory and a Parameterized test?

I'm not interested in implementation differences when creating the test classes, just when you would choose one over the other.

Java Solutions


Solution 1 - Java

From what I understand: With Parameterized tests you can supply a series of static inputs to a test case.

Theories are similar but different in concept. The idea behind them is to create test cases that test on assumptions rather than static values. So if my supplied test data is true according to some assumptions, the resulting assertion is always deterministic. One of the driving ideas behind this is that you would be able to supply an infinite number of test data and your test case would still be true; also, often you need to test an universe of possibilities within a test input data, like negative numbers. If you test that statically, that is, supply a few negative numbers, it is not guaranteed that your component will work against all negative numbers, even if it is highly probable to do so.

From what I can tell, xUnit frameworks try to apply theories' concepts by creating all possible combinations of your supplied test data.

Both should be used when approaching a scenario in a data-driven scenario (i.e only inputs change, but the test is always doing the same assertions over and over).

But, since theories seem experimental, I would use them only if I needed to test a series of combinations in my input data. For all the other cases I'd use Parameterized tests.

Solution 2 - Java

Parameterized.class tests "parametrize" tests with a single variable, while Theories.class "parametrize" with all combinations of several variables.

For examples please read:

http://blogs.oracle.com/jacobc/entry/parameterized_unit_tests_with_junit

http://blog.schauderhaft.de/2010/02/07/junit-theories/

http://blogs.oracle.com/jacobc/entry/junit_theories

Theories.class is similar to Haskell QuickCheck:

http://en.wikibooks.org/wiki/Haskell/Testing

but QuickCheck autogenerates parameter combinations

Solution 3 - Java

In addition to above responses: On a input with 4 values and 2 test methods

  • @RunWith(Theories.class) - will generate 2 JUnit tests

  • @RunWith(Parameterized.class) - will generate 8 (4 inputs x 2 methods) JUnit tests

Solution 4 - Java

A little late in replying. But it would be helpful to the future testers.

Parameterized Tests vs Theories

  • Class annotated with "@RunWith (Parameterized.class)" VS "@RunWith(Theories.class)"
  • Test inputs are retrieved from a static method returning Collection and annotated with @Parameters vs static fields annotated with @DataPoints or @DataPoint.
  • Inputs are passed to the constructor (mandatory) and used by the test method vs inputs are directly passed to the test method.
  • Test method is annotated with @Test and doen't take arguments vs Test method is annotated with @Theory and may take arguments

Solution 5 - Java

From my understanding the difference is that a Parameterized Test is used when all you want to do is test a different set of inputs (test each one individually), a Theory is a special case of a Parameterized Test in which you are testing every input as a whole (every parameter needs to be true).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondogbaneView Question on Stackoverflow
Solution 1 - JavaFabio KenjiView Answer on Stackoverflow
Solution 2 - JavagliptakView Answer on Stackoverflow
Solution 3 - JavaandreyroView Answer on Stackoverflow
Solution 4 - JavaVHSView Answer on Stackoverflow
Solution 5 - JavaRicardo GomesView Answer on Stackoverflow