Difference between JUnit Theories and Parameterized Tests
Asked Answered
N

5

62

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.

Nigelniger answered 20/4, 2011 at 8:37 Comment(1)
I used parametrized before but found it can get a bit messy. Theory seems to do the same thing but in a cleaner way. Perhaps it is a second attempt to get the concept of parametrized tests right. Good question, thanks for raising it !Conformable
S
30

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.

Stryker answered 3/5, 2011 at 23:28 Comment(1)
github.com/BartoszMiller/junit4-explored/blob/master/src/test/… - It is a basic sample test which executes one @Theory for 10000 different input data thanks to overriding abstract ParameterSupplier. I'm not sure if it can achieved as easily with parameterized runner.Mistrial
T
12

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

Toxicity answered 29/4, 2011 at 15:10 Comment(1)
That's not true. You can have parameterised tests with any number of variables (parameters) and a Theory test with a single parameter. @Fabio Kenji's answer above explains the real difference well.Surfboarding
W
3

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

Warnke answered 19/9, 2017 at 12:52 Comment(0)
S
1

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
Slayton answered 8/5, 2017 at 17:56 Comment(0)
E
0

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).

Egress answered 20/4, 2011 at 22:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.