Geoffrey De Smet
Geoffrey De Smet

Reputation: 27312

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.

Upvotes: 32

Views: 14282

Answers (5)

user7610
user7610

Reputation: 28811

Similarly to Miguel's answer, if you are using the JUnit 5's

@ParameterizedTest
@CsvFileSource(resources = arrayOf("/sender.csv"))

you can go to your csv file and "comment out" some lines by prepending the # character to them.

Upvotes: 2

Moritz Eysholdt
Moritz Eysholdt

Reputation: 811

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.

Upvotes: 5

Miguel Pereira
Miguel Pereira

Reputation: 1791

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;

Upvotes: 2

duemir
duemir

Reputation: 1316

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.

Upvotes: 3

Duncan Jones
Duncan Jones

Reputation: 69339

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).

Upvotes: 23

Related Questions