Running tests in parallel with Junit

I would like to run every method annotated with @Test through several classes at the same time. In some cases, I would like to limit this and say that only 100 people can start at any given time. I would like methods with @BeforeClass annotations to be run once before any test in the class was run, and I would like @AfterClass annotations to be run once after all the tests in the class. I would like System.out , System.err and Exceptions to be buffered / caught appropriately, rather than written out so that they don't interleave, and I can read the final output and understand what happened.

Does it exist? I have a large number of independent test cases, and my application (I believe) is thread safe. None of these tests have JVM dependencies, and I want to finish them as quickly as possible, given my hardware.

If this does not exist, is there a specific reason why not? How much time do junit users lose in time because it is not easy? Can I build it in Junit? In my opinion, it should be as simple as a single flag, and it "just works."

+7
java junit testing
source share
3 answers

You can accomplish this with the JUnit ParallelComputer (note that it is still considered experimental). This is a fairly simple implementation that is supported by the java.util.concurrent.ExecutorService API.
If you are interested in how this works, check out the source .

Basically you call JUnitCore.runClasses(Computer, Classes ...) and pass in a ParallelComputer object for the first argument.

Usage example:

 import org.junit.Test; import org.junit.experimental.ParallelComputer; import org.junit.runner.JUnitCore; public class ParallelComputerExample { @Test public void runAllTests() { Class<?>[] classes = { ParallelTest1.class, ParallelTest2.class }; // ParallelComputer(true,true) will run all classes and methods // in parallel. (First arg for classes, second arg for methods) JUnitCore.runClasses(new ParallelComputer(true, true), classes); } public static class ParallelTest1 { @Test public void test1a() { lookBusy(3000); } @Test public void test1b() { lookBusy(3000); } } public static class ParallelTest2 { @Test public void test2a() { lookBusy(3000); } @Test public void test2b() { lookBusy(3000); } } public static void lookBusy(long ms) { try { Thread.sleep(ms); } catch (InterruptedException e) { System.out.println("interrupted"); } } } 

The above code will work after 3 seconds, since all methods and classes are executed in parallel.

This will work after 6 seconds (because all classes are parallel). JUnitCore.runClasses(new ParallelComputer(true, false), classes);

This will also work after 6 seconds (because all methods are parallel). JUnitCore.runClasses(new ParallelComputer(false, true), classes);

+11
source share

Yes, you can.

If you are using maven. You can use

Maven-error-free plugin

In Spring,

You can check this link

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.1</version> <configuration> <parallel>classes</parallel> <threadCount>5</threadCount> </configuration> </plugin> </plugins> </build> 

Solution 2: Junit4 provides a parallel function using ParallelComputer

+2
source share

The JUnit Toolbox provides JUnit runners for running tests in parallel.

In order not to mix the output with System.err and System.out , you must run the tests in separate JVMs because System.err and System.out are global.

0
source share

All Articles