Parameterized jUnit tests in Java: Combine 'custom test name' feature (@Parameters(name="namestring")) in jUnit 4.11 with junitparams @FileParameter?

StackOverflow https://stackoverflow.com/questions/22314505

Question

With Parameterized tests in Java, can the 'custom test name' feature exposed in jUnit 4.11 (@Parameters(name="namestring")) be combined with any of the features from Google junitparams?

I have a Java test which cycles through a file containing a set of view names for comparison. Instead of reporting a single test, want each comparison to report out as a separate test instance, each test name customized for the view name supplied as input.

Ideally, would like to use junitparams @FileParameter to load in a file containing the set of names, passing strings into jUnit 'name=' to use as test names and also into the test as input. Both the '@FileParameter' and 'name=' features are relatively simple to implement independently.

Per its doc page, junitparams is 'compliant' with jUnit 4.11, but I haven't figured out a way to combine the two features above. While I can supply both @FileParameter and @Parameters notations to the same test without a syntax or runtime error, the run result seems to ignore the presence of the latter.

Has anyone done this? Is there a better / simpler option? First real question to the Exchange, so please bear with me...

=cjs

Was it helpful?

Solution

JUnit tests that use @Parameters annotation must be run with

@RunWith(Parameterized.class)

However junitparams uses it's own runner

@RunWith(JUnitParamsRunner.class)

A JUnit test can only use a single runner so I don't see how they both can work together unless someone merges the two runners into one.

The easiest solution is to forego junitparams and implement the logic that reads data from the input file and turns it into a parameter list so you can return that from a method annotated with @Parameters.

Another solution is to modify JUnitParamsRunner to behave more like the Parameterized runner does e.g. generate a test with a different name for each parameter read from the input file.

See also:

OTHER TIPS

Looking at it again, I agree. A little finagling, found junitparams not needed to do what I wanted.

Ugly partial code sample:

@RunWith(Parameterized.class) public class ParamSampleTest {

@Parameters(name = "{index} myTest : using [arg0 {0}] and [arg1 {1}]")
public static Collection<Object[]> data() throws FileNotFoundException {
    return ParamSampleTest.mydataset();
}

private static Collection<Object[]> mydataset() throws FileNotFoundException {
    <snip data read, get length>
    ArrayList<Object[]> myList = new ArrayList<Object[]>(length);
    <snip create String[][], populate, add to myList>
    return myList;          // return ArrayList of Object[]
}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top