开发者

How to make JUnit test cases execute in parallel? [duplicate]

This question al开发者_JAVA技巧ready has answers here: Closed 10 years ago.

Possible Duplicate:

Running junit tests in parallel?

I found the test cases inside jUnit are executed in sequence, how to make them execute in parallel?


Junit4 provides parallel feature using ParallelComputer:

public class ParallelComputerTest {  

   @Test  
   public void test() {      
      Class[] cls={ParallelTest1.class,ParallelTest2.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);     
   } 

   public static class ParallelTest1 {  
      @Test public void a(){}  
      @Test public void b(){}  
   }  

   public static class ParallelTest2 {  
      @Test public void a(){}  
      @Test public void b(){}  
   }  
} 


Here is some sample code. This works for me really well. ExecutorService.

public class TestCases {
    static ExecutorService exe ;
    public static void main(String[] args) throws Throwable {
        test1() ;
        test2() ;
        test3() ;
    }
    public static void test1() {
       exe = Executors.newCachedThreadPool() ;
       for (int i = 0 ; i < 10 ; i++) {
           Test1 test1 = new Test1() ;
           exe.execute(test1) ;
       }
       exe.shutdown() ;
       while(!exe.isShutDown()) {
       }
    }
   //same for test2 and test3
}

public class Test1 implements Runnable {
    public Test1() {
    }
    @Test
    public myTest throws Throwable {
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜