Skip to content

Instantly share code, notes, and snippets.

@djangofan
Last active October 25, 2018 10:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save djangofan/5006098 to your computer and use it in GitHub Desktop.
Save djangofan/5006098 to your computer and use it in GitHub Desktop.
JUnit Parameterized AND Parallel test example
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;
public class AParallelSuite {
// this method of executing JUnit tests is not recommended if you use a parallel
// capable runner such as Maven, Gradle, XStudio, or Ant, etc.
@Test
public void test() {
Class[] cls={JUnitTest6.class,JUnitTest5.class };
//Parallel among classes
JUnitCore.runClasses(ParallelComputer.classes(), cls);
//Parallel among methods in a class
//JUnitCore.runClasses(ParallelComputer.methods(), cls);
//Parallel all methods in all classes
//JUnitCore.runClasses(new ParallelComputer(true, true), cls);
}
}
....
// runs a filtered test class with Gradle JUnit runner
task runAParallelSuite(type: Test) {
// uncomment maxParallelForks if you prefer to use the Gradle process forker
// which also requires a complete change of how the suite class works
// maxParallelForks = 4
include '**/AParallelSuite.class'
testReportDir = file("${reporting.baseDir}/AParallelSuite")
testResultsDir = file("${buildDir}/test-results/AParallelSuite")
}
....
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* JUnit Parameterized Test
* This is one of multiple JUnitTest classes
* @author Jon Austen
*
*/
@RunWith(value = Parameterized.class)
public class JunitTest5 {
private int number;
public JunitTest5(int number) {
this.number = number;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized Number is : " + number);
}
}
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* JUnit Parameterized Test
* This is one of multiple JUnitTest classes
* @author Jon Austen
*
*/
@RunWith(value = Parameterized.class)
public class JunitTest6 {
private int number;
public JunitTest6(int number) {
this.number = number;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
return Arrays.asList(data);
}
@Test
public void pushTest() {
System.out.println("Parameterized Number is : " + number);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment