The difference between Unit theories and parameterized tests

What is the difference between theory and parameterized test?

I'm not interested in implementation differences when creating test classes, only when you select one of them.

+57
java junit4 parameterized
Apr 20 2018-11-11T00:
source share
5 answers

From what I understand: With parameterized tests, you can put a number of static inputs into a test file.

Theories are similar, but different in concept. The idea behind them is to create test cases that are tested based on assumptions, not static values. Therefore, if my test data provided is true in accordance with some assumptions, the obtained statement is always deterministic. One of the driving ideas is that you can provide an infinite amount of test data, and your test case will still be true; In addition, often you need to check all the possibilities of the possibilities in the test input, for example, negative numbers. If you check it statically, that is, put a few negative numbers, it is not guaranteed that your component will work against all negative numbers, even if it is very likely.

From what I can tell, xUnit frameworks are trying to apply theories of theories by creating all possible combinations of the test data you provide.

Both should be used when approaching the scenario in a data-driven scenario (i.e. only the inputs are changed, but the test always makes the same statements over and over).

But since theories seem experimental, I would only use them if I needed to check the sequence of combinations in my input. For all other cases, I would use Parameterized Tests.

+26
May 03 '11 at 23:28
source share
β€” -

Parameterized.class tests the tests "parameterize" with a single variable, while Theories.class "parameterizes" 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 automatically generates combinations of parameters

+11
Apr 29 2018-11-11T00:
source share

In my opinion, the difference is that the Parameterized Test parameter is used when all you want to do is test a different set of inputs (test each separately), Theory is a special case of a Parameterized test in which you test each input as a whole (each parameter must be true).

0
Apr 20 '11 at 10:18
source share

A little late in return. But this would be useful for future testers.

Parameterized tests against theories

  • Class annotated with "@RunWith (Parameterized.class)" VS "@RunWith (Theories.class)"
  • Test inputs are retrieved from a static method that returns Collection and annotated with @Parameters against static fields annotated with @DataPoints or @DataPoint.
  • The inputs are passed to the constructor (required) and used by the testing method. Inputs are passed directly to the test method.
  • The test method is annotated with @Test and doen't accepts arguments vs The test method is annotated with @Theory and can take arguments
0
May 08 '17 at 17:56
source share

In addition to the answers above: 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

0
Sep 19 '17 at 12:52 on
source share



All Articles