Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

junit

maven-2

jvm

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

like image 296
Boris Pavlović Avatar asked Jul 25 '11 08:07

Boris Pavlović


People also ask

Do JUnit tests run concurrently?

Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.

Does Maven run JUnit tests in parallel?

Maven Dependencies In a nutshell, Surefire provides two ways of executing tests in parallel: Multithreading inside a single JVM process. Forking multiple JVM processes.

How do I run parallel execution in JUnit?

By enabling parallel execution, the JUnit engine starts using the ForkJoin thread pool. Next, we need to add a configuration to utilize this thread pool. We need to choose a parallelization strategy. JUnit provides two implementations (dynamic and fixed) and a custom option to create our implementation.

Does surefire run tests in parallel?

The surefire offers a variety of options to execute tests in parallel, allowing you to make best use of the hardware at your disposal. But forking in particular can also help keeping the memory requirements low.


1 Answers

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)
like image 83
FrVaBe Avatar answered Oct 19 '22 20:10

FrVaBe