开发者

How to tell Maven2 to execute jUnit tests one by one each in new JVM instance?

开发者 https://www.devze.com 2023-03-22 11:56 出处:网络
Is it po开发者_StackOverflow中文版ssible to tell Maven2 to execute every jUnit test in new JVM instance (fork) in serial mode, i.e. one by one.You have to fork the JVM like explained here

Is it po开发者_StackOverflow中文版ssible to tell Maven2 to execute every jUnit test in new JVM instance (fork) in serial mode, i.e. one by one.


You have to fork the JVM like explained here

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.9</version>
  <configuration>
    <forkMode>always</forkMode>
  </configuration>
</plugin>

It should also be possible by just declaring a Sytem property

mvn -DforkMode=always test

As described in the documentation: "always" forks for each test-class. I do not know if the "pertest" setting will fork for each test.


Thanks to @Djebel for pointing out that forkMode is deprecated now. There is a detailed documentation on "Fork Options and Parallel Test Execution" and how to use the new parameters forkCount and reuseForks and that also includes the following migration tips:

Old Setting                         New Setting
forkMode=once (default)             forkCount=1 (default), reuseForks=true (default)
forkMode=always                     forkCount=1 (default), reuseForks=false
forkMode=never                      forkCount=0
forkMode=perthread, threadCount=N   forkCount=N, (reuseForks=false, if you did not had that one set)


What about the standard forkMode option? Does it run the tests in parallel as opposed to serial as you want it?

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <forkMode>always</forkMode>
  </configuration>
</plugin>


Well i tried these responses but what i just got was several test functions of a same JUnit test running at the same time. For creating a JVM for each JUnit Test files (what i needed) you must use the folowing configuration (The difference it the parallel parameter):

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.9</version>
        <configuration> 
            <parallel>classes</parallel>
            <reuseForks>false</reuseForks>
            <includes> 
                <include>**/*Test.java</include>     
            </includes> 
        </configuration>
    </plugin>

Replace the mattern **/*Test.java for one that matches those JUnit Tests that must run in different JVMs. For more information visit: Combining forkCount and parallel.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号