简体   繁体   中英

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.

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.

测试输出

Result:

只测试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).

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.

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.

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;

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM