Last active
October 25, 2018 10:53
-
-
Save djangofan/5006098 to your computer and use it in GitHub Desktop.
JUnit Parameterized AND Parallel test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.... | |
// 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") | |
} | |
.... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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