2012-11-22 127 views
9

我想知道是否可以在定義參數化測試時以編程方式並行運行JUnit測試。這個想法將能夠像Eclipse中的常規JUnit測試一樣運行它們。在Eclipse中並行運行JUnit參數化測試

我當前的代碼是類似於:

@RunWith(Parameterized.class) 
public class JUnitDivideClassTests { 

    @Parameters 
    public static Collection<Object[]> data() { 

     return Arrays.asList(new Object[][] { { 12, 3, 4 }, { 12, 2, 6}, { 12, 4, 3 }}); 
    } 

    private int n; 
    private int d; 
    private int q; 

    public JUnitDivideClassTests(int n, int d, int q) { 

     this.n = n; 
     this.d = d; 
     this.q = q; 
    } 

    @Test 
    public void test() { 

     Assert.assertEquals(q, n/d); 
    } 
} 

如發現@http://codemadesimple.wordpress.com/2012/01/17/paramtest_with_junit/

回答

6

經過一番搜索,我發現,我不得不執行(或者更確切地說,使用)下面的代碼:

public class ParallelizedParameterized extends Parameterized { 
    private static class ThreadPoolScheduler implements RunnerScheduler { 
     private ExecutorService executor; 

     public ThreadPoolScheduler() { 
      String threads = System.getProperty("junit.parallel.threads", "16"); 
      int numThreads = Integer.parseInt(threads); 
      executor = Executors.newFixedThreadPool(numThreads); 
     } 

     @Override 
     public void finished() { 
      executor.shutdown(); 
      try { 
       executor.awaitTermination(10, TimeUnit.MINUTES); 
      } catch (InterruptedException exc) { 
       throw new RuntimeException(exc); 
      } 
     } 

     @Override 
     public void schedule(Runnable childStatement) { 
      executor.submit(childStatement); 
     } 
    } 

    public ParallelizedParameterized(Class klass) throws Throwable { 
     super(klass); 
     setScheduler(new ThreadPoolScheduler()); 
    } 
} 

@http://hwellmann.blogspot.pt/2009/12/running-parameterized-junit-tests-in.html

+0

它的工作原理,謝謝 –