Boris Pavlović
Boris Pavlović

Reputation: 64640

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

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

Upvotes: 15

Views: 14444

Answers (3)

EliuX
EliuX

Reputation: 12685

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.

Upvotes: 1

FrVaBe
FrVaBe

Reputation: 49351

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)

Upvotes: 30

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340953

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>

Upvotes: 2

Related Questions