JUnit: running concurrent tests

I'm still pretty new to Java programming and JUnit testing. I am using NetBeans 6.9.1, which comes with junit-4.5 (but I added junit-4.8.2 to my library).

I have several test classes, and in each class there are a number of @Test methods.

When I run a specific test class, it runs through each @Test method one at a time. I also created a test suite with

@RunWith(Suite.class) @Suite.SuiteClasses(value = { TestClassA.class, TestClassB.class, TestClassC.class}) public class NewTestSuite { } 

which will go through each of my test classes and in each run each @Test method.

My question is: is it possible for me to run test classes at the same time? Or, within each test class, can @Test methods be run simultaneously?

This will allow me to run all the tests much faster than the classes and methods run once.

Thanks!

+4
source share
2 answers

Use org.junit.experimental.ParallelComputer : Example:

  public class NewTestSuite { public static void main(String[] s){ Class[] cls={TestClassA.class,TestClassB.class,TestClassB.class }; //simultaneously all methods in all classes Result result = JUnitCore.runClasses(new ParallelComputer(true, true), cls); System.out.print(result.wasSuccessful()); //simultaneously among classes //Result result = JUnitCore.runClasses(ParallelComputer.classes(), cls); //simultaneously among methods in a class //Result result = JUnitCore.runClasses(ParallelComputer.methods(), cls); } } 
+7
source

You can try this simple example: I am adding a statement because in JUnitCore we do not have an assembly.

 public class TestParallelExecution { private Logger logger = LoggerFactory.getLogger(TestParallelExecution.class); @Test public void runAllTest(){ //add test class Class[] testClasses={testClass1.class, testClass2.class, }; //Parallel execute only classes Result resultClasses = JUnitCore.runClasses(ParallelComputer.classes(), testClasses); //Parallel execute only methods in classes Result result = JUnitCore.runClasses(ParallelComputer.methods(), testClasses); //Run Parallel all methods in all test classes which declare in testClasses[] //method accept new ParallelComputer(classes, methods) Result result = JUnitCore.runClasses(new ParallelComputer(true, true), testClasses); List<Failure> failures = result.getFailures(); if(result1.wasSuccessful() != true){ StringBuilder sb = new StringBuilder(); for(int i = 0; i < failures.size(); i++){ sb.append(System.lineSeparator()); sb.append("<---------------New test method--------------->"); sb.append(System.lineSeparator()); sb.append(failures.get(i).toString()); sb.append(System.lineSeparator()); } File file = new File("C:\\..\\FailedTest.txt"); try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { writer.write(sb.toString()); writer.close(); } catch (IOException e) { logger.trace("I can't create file,because : ", e); } //logger.error(sb.toString()); assertTrue(false); }else { assertTrue(true); } } } 
+1
source

Source: https://habr.com/ru/post/1340315/


All Articles