2012-11-22 24 views
9

Tôi đã tự hỏi liệu có thể chạy các kiểm tra JUnit một cách lập trình song song khi xác định các phép kiểm tra tham số. Ý tưởng sẽ là sau đó có thể chạy chúng giống như các bài kiểm tra JUnit thông thường trong Eclipse.Chạy các kiểm tra tham số JUnit trong Parallel trong Eclipse

mã hiện tại của tôi là một cái gì đó tương tự như:

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

như được tìm thấy @http://codemadesimple.wordpress.com/2012/01/17/paramtest_with_junit/

Trả lời

6

Sau khi một số tìm kiếm, tôi thấy rằng tôi chỉ phải thực hiện (hay đúng hơn, sử dụng) đoạn mã sau:

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

nó hoạt động, nhờ –

Các vấn đề liên quan