This test class:
@RunWith(Theories.class)
public class TheoriesConfusion
{
@DataPoints
public static int[] ints()
{
System.out.println("Generator called");
return new int[]{1, 2, 3, 4, 5};
}
@Theory
public void twoArgTest(int x, int y)
{
assertTrue(x < y || x >= y);
}
}
Prints the following output:
Generator called
Generator called
Generator called
Generator called
Generator called
Generator called
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.137 sec
This is quite counterintuitive, as I expect the data-generating function to be called only once. This has implications when creating random data, or any case where the data-generating method returns different results on each call, so I'd like to understand it.
After some experimentation, I've found that testing an array of length n
against a Theory with c
args, the generate function is called x times, where x = n^c + n^(c-1) + ... + n^0
.
The source is a little difficult to comprehend, but my assumption is that it works something like this (pseudocode):
for firstArg in generateArgs():
for secondArg in generateArgs():
for thirdArg in generateArgs():
testTheory(firstArg, secondArg, thirdArg)
Which makes some sense, basically it's just not caching the results of the method, so if you want the method to be called just once, you have to annotate a static field, like:
@DataPoints
public static int[] ints = ints();
public static int[] ints()
{
System.out.println("Generator called");
return new int[]{1, 2, 3, 4, 5};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.