9

I was wondering if it's possible to run JUnit tests programatically in parallel when defining parameterized tests. The idea would be to then be able to run them just as regular JUnit tests in Eclipse.

My current code is something similar to:

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

as found @ http://codemadesimple.wordpress.com/2012/01/17/paramtest_with_junit/

asked Nov 22, 2012 at 1:47

1 Answer 1

8

After some searching, I found that I just had to implement (or rather, use) the following code:

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

answered Nov 23, 2012 at 3:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.