Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JUnit Theories and Parameterized Tests

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.

like image 914
dogbane Avatar asked Apr 20 '11 08:04

dogbane


People also ask

What is theories in JUnit?

The Theories runner allows to test a certain functionality against a subset of an infinite set of data points. A Theory is a piece of functionality (a method) that is executed against several data inputs called data points. To make a test method a theory you mark it with @Theory.

What is JUnit parameterized test?

JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values. There are five steps that you need to follow to create a parameterized test. Annotate test class with @RunWith(Parameterized. class).

What is a parameterized test case?

Parameterized tests are a good way to define and run multiple test cases, where the only difference between them is the data. They can validate code behavior for a variety of values, including border cases. Parameterizing tests can increase code coverage and provide confidence that the code is working as expected.

What is parameterization in testing?

Test parameterization is a type of data-driven testing that allows you to execute the same test, multiple times using different parameters. Xray Cloud has a parameterized tests feature, that executes the same test with different input values, multiple times, without ever having to clone or replicate it.


2 Answers

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.

like image 145
Fabio Kenji Avatar answered Sep 17 '22 20:09

Fabio Kenji


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

like image 20
gliptak Avatar answered Sep 17 '22 20:09

gliptak