Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit: Run simultaneous tests

I am still fairly new to Java programming and to JUnit testing. I use NetBeans 6.9.1 which comes with junit-4.5 (but I have added junit-4.8.2 to my library).

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

When I run a particular Test class it runs through each @Test method one at a time. I have also created a Test Suite with

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

which will run through each of my Test Classes and within each run each @Test method.

My question is: is it possible for me to run the Test Classes simultaneously? Or, within each Test Class is it possible to run the @Test methods simultaneously?

Doing so would allow me to run through all of the tests much faster than having the classes and methods run one-at-a-time.

Thanks!

like image 744
kmccoy Avatar asked Feb 18 '11 13:02

kmccoy


People also ask

Do JUnit tests run concurrently?

Once parallel test execution property is enabled, the JUnit Jupiter engine will execute tests in parallel according to the provided configuration with declared synchronization mechanisms.

How do I run parallel execution in JUnit?

By enabling parallel execution, the JUnit engine starts using the ForkJoin thread pool. Next, we need to add a configuration to utilize this thread pool. We need to choose a parallelization strategy. JUnit provides two implementations (dynamic and fixed) and a custom option to create our implementation.

Does JUnit 4 run tests in parallel?

A plugin that allows you to run JUnit4 tests in parallel (using multiple CPU cores/threads).


2 Answers

Use org.junit.experimental.ParallelComputer: Sample:

    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);  
      }
   } 
like image 191
卢声远 Shengyuan Lu Avatar answered Oct 09 '22 01:10

卢声远 Shengyuan Lu


You can try this simple example: I add a assertion,because in JUnitCore,we don't have a build in.

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);
        }
    }
}
like image 33
Zhivko.Kostadinov Avatar answered Oct 09 '22 02:10

Zhivko.Kostadinov