JUnit Tutorial
Introduction to Junit JUnit Set up JUnit Architecture JUnit Annotations JUnit Fixtures Junit Assertions Junit Categories @Test Annotation Parameters Verification of Exceptions Ignoring tests Time out in JUnit tests Parameterizing tests Test Suite TestWatcher TemporaryFolder ExternalResource Theories in JUnit JUnit Test Runners Execution order of JUnit tests Assumptions in JUnit JUnit and Hamcrest Matchers Running JUnit tests in parallel JUnit and Maven Integration JUnit and Gradle Integration Executing Selenium tests using JUnit test framework Method interceptor and usage JUnit in Intellij IDEA JUnit in EclipseRunning tests in parallel in junit
We can run tests in parallel in Junit by 2 ways.- using org.junit.experimental.ParallelComputer class.
- By configuring maven surefire plug-in
1. using org.junit.experimental.ParallelComputer
Below example explains how to run multiple classes and methods in Parallel.
package parallel;
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;
/**
* Created by Sagar on 24-04-2016.
*/
public class ParallelTests {
@Test
public void test() {
Class[] testClasses = {ParallelTestClassA.class,ParallelTestClassB.class };
System.out.println("running multiple classes serially");
JUnitCore.runClasses(ParallelComputer.serial(), testClasses);
System.out.println("running multiple classes in parallel");
JUnitCore.runClasses(ParallelComputer.classes(), testClasses);
System.out.println("running methods in a class in parallel");
JUnitCore.runClasses(ParallelComputer.methods(), testClasses);
System.out.println("running methods as well classes in parallel");
JUnitCore.runClasses(new ParallelComputer(true, true), testClasses);
}
public static class ParallelTestClassA {
@Test
public void testA1(){
System.out.println("Running A1");
}
@Test
public void testA2(){
System.out.println("Running A2");
}
}
public static class ParallelTestClassB {
@Test public void testB1(){
System.out.println("Running B1");
}
@Test public void testB2(){
System.out.println("Running B2");
}
}
}
Here is the output of above example.
running multiple classes serially
Running A1
Running A2
Running B1
Running B2
running multiple classes in parallel
Running B1
Running B2
Running A1
Running A2
running methods in a class in parallel
Running A1
Running A2
Running B1
Running B2
methods as well classes in parallel
Running B2
Running B1
Running A1
Running A2
using Maven surefire plug-in
We can configure surefire plugin as shown below to run tests in parallel. In below configuration, 10 threads will be created at the same time – 1 for each test method. "parallel" tag can take various values like classes, suites, both, all etc.
<configuration>
<includes><include>%regex[.*]</include></includes>
<parallel>methods</parallel>
<threadCount>10</threadCount>
</configuration>
Forked Execution
In forked execution, multiple JVMs are launched to execute the tests in separate processes.Web development and Automation testing
solutions delivered!!