开发者

JUnit: Run simultaneous tests

I am still fairly new to Java programming and t开发者_如何学编程o 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!


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);  
      }
   } 


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);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜