Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit parameterized tests: how do I run only 1 specific test from IntelliJ/Eclipse?

I have a @Parameterized junit test that spawns 50 tests:

@RunWith(Parameterized.class)
public class NurseRosteringSolveAllTurtleTest ... {

    @Parameterized.Parameters(name = "{index}: {0}")
    public static Collection<Object[]> getSolutionFilesAsParameters() {
        return ... // returns 50 Files.
    }

    public NurseRosteringSolveAllTurtleTest(File unsolvedDataFile) {
        ...
    }

    ...

    @Test
    public void solveDataFile() {
        ...
    }

}

Running it takes an hour (and it's impossible to shorten that time, they are integration tests). Test 28 fails.

How do I run test 28 alone, without running the other 49 tests? Without changing the actual code, by simply configuring a -D or something similar in IntelliJ's (or Eclipse's) run configuration.

like image 369
Geoffrey De Smet Avatar asked May 13 '13 08:05

Geoffrey De Smet


4 Answers

I just tested this in Eclipse with a simple parameterized test that always fails on test #4. One is able to right-click on the failed test and select Run. Only that test then executes.

test output

Result:

just test 4

Frustratingly, I can't see what Eclipse did to solve the problem. Nothing is apparently altered in the run configuration. In particular, if you select to run the configuration a second time, it executes all the tests.

Some further testing shows that Eclipse will regenerate all 10 parameter values, but only uses the 4th value. (This was determined by embedding a print statement in the @Parameters method).

like image 197
Duncan Jones Avatar answered Nov 13 '22 04:11

Duncan Jones


Eclipse is now (as of the Mars M4 release) able to run not just a single test from the Parameterized test class but any kind of subtree.

This can be:

  • all methods for a single data set as returned by the @Parameterized-method
  • all datasets for a single @Test-method

And as already mentioned, the test can also be specified by entering the tests name into the "method" text filed within the launch configuration. There will be a marker indicating that the method doesn't exist, but the test will run anyway.

See this blog post for details.

like image 33
Moritz Eysholdt Avatar answered Nov 13 '22 06:11

Moritz Eysholdt


Not sure if it will help, but you can try a trick which I used with Eclipse and JUnit parameterized tests.

In JUnit launch configuration in "Test method" field you can write the full name of parameterized test, in your example it should be something like this 'solveDataFile[28: /path/to/your/file]'. Eclipse will complain that method does not exist but will still lunch it successfully.

like image 3
duemir Avatar answered Nov 13 '22 06:11

duemir


For a subset of tests ex( 27 & 28 ) Just add:

`.subList( startInclusive, stopExclusive );`

before returning your parameters collection.

Non consecutive subsets:

Collection<Object[]> c = Arrays.asList( data ).subList( startInclusive, stopExclusive );
c.add( another subset );
return c;
like image 2
Miguel Pereira Avatar answered Nov 13 '22 05:11

Miguel Pereira