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

前端 未结 3 1646
夕颜
夕颜 2020-12-15 23:13

Is it possible to tell Maven2 to execute every jUnit test in new JVM instance (fork) in serial mode, i.e. one by one.

相关标签:
3条回答
  • 2020-12-15 23:55

    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 讨论(0)
  • 2020-12-16 00:03

    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)
    
    0 讨论(0)
  • 2020-12-16 00:04

    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>
    
    0 讨论(0)
提交回复
热议问题