简体   繁体   中英

Run JUnit test *methods* in isolation

Is maven-surefire-plugin able to run all JUnit test methods in isolation? Ie, is it able to fork a JVM for each test method rather than for each test class?

The deprecated option

<forkMode>pertest</forkMode>

and the current

<forkCount>1</forkCount>
<reuseForks>false</reuseForks>

seem to only fork for each test class.

PS: Test methods should be independent and therefore no one should need to run each one on a new JVM (and not to say that it would be very expensive). But I was wondering whether there is such option or not.

The Maven Surefire Plugin supports this configuration:

<parallel>methods</parallel>

For JUnit versions >= 4.7

For example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
      <parallel>methods</parallel>
      <threadCount>10</threadCount>
    </configuration>
  </plugin>

More details here .

Edit 1 based on your comment:

I would like to run each test method on a new JVM, and not on another thread on the same JVM

When using reuseForks=true and a forkCount > 1, test classes are handed over to the forked process one-by-one. So, with parallel=methods classes are executed in forkCount concurrent processes, each of these processes uses a threadCount number threads to execute the methods of one class in parallel.

So, it looks like surefire does not support your use case.

FWIW, spawning a JVM for every test method sounds like it could get expensive. Are you perhaps dealing with an issue where test methods have unwanted side effects within a JVM (maybe they set System properties or change statics) and these side effects cannot be isolated? If so, perhaps revisting the tests to eliminate these side effects might be more desireable than implementing a bespoke and potentially expensive test runner?

I found this project which claims to do it, though it did not seem to work when I tried (maybe out of date). Anyway I imagine this could not work for anything but basic @Test -annotated methods: no @RunWith(Parameterized.class) etc.

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