To run junit testcases parallel, you can create your own class to run junit with:
Add this tag to your class declaration.
Implementation of this class looks like:
Now inside your test class, you will need to create a method with
public PocAddPoiTest(String browser) {
super();
this.browser = browser;
}
@Parameters
public static Collection browsersStrings() {
// return Arrays.asList(new Object[][] { { "*firefox" },
// { "*googlechrome" } });
return Arrays.asList(new Object[][] { { "*firefox","*iexplore" } });
}
When junit gets started, it will run two tests parallel.
Not sure how it handles output .. :-).
Add this tag to your class declaration.
@RunWith(Parallelized.class)
Implementation of this class looks like:
package mypackage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized 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);
}
public void finished()
{
executor.shutdown();
try
{
executor.awaitTermination(10, TimeUnit.MINUTES);
}
catch (InterruptedException exc)
{
throw new RuntimeException(exc);
}
}
public void schedule(Runnable childStatement)
{
executor.submit(childStatement);
}
}
public Parallelized(Class klass) throws Throwable
{
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized 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);
}
public void finished()
{
executor.shutdown();
try
{
executor.awaitTermination(10, TimeUnit.MINUTES);
}
catch (InterruptedException exc)
{
throw new RuntimeException(exc);
}
}
public void schedule(Runnable childStatement)
{
executor.submit(childStatement);
}
}
public Parallelized(Class klass) throws Throwable
{
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
@Parameters
tag declaration, that feeds the constructor:public PocAddPoiTest(String browser) {
super();
this.browser = browser;
}
@Parameters
public static Collection browsersStrings() {
// return Arrays.asList(new Object[][] { { "*firefox" },
// { "*googlechrome" } });
return Arrays.asList(new Object[][] { { "*firefox","*iexplore" } });
}
When junit gets started, it will run two tests parallel.
Not sure how it handles output .. :-).
Comments