JUnit parallel tests with parameters

So, I am trying to run parallel parameterized tests. I have a solution where the same test can be run in parallel with the parameters specified, for example, I have the following:

@Test
public void someTest1(){
}

@Test
public void someTest2(){
}

I can make someTest1 () work with all the parameters at the same time, but someTest2 () will still have to wait until someTest1 () will execute all the parameters before executing. I was wondering if anyone knows of a solution to be able to run someTest1 () with all parameters and someTest2 () with all parameters at the same time? I tried tempus-fugit for simultaneous testing , which is great for tests that are not parameterized ...

Below is the code that I have for the current check of each parameter in parallel.

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;

/**
 * Class used from the following source:
 * http://jankesterblog.blogspot.com/2011/10
 * /junit4-running-parallel-junit-classes.html
 * 
 * @author Jan Kester
 * 
 */
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(12, TimeUnit.HOURS);
            } catch (InterruptedException exc) {
                throw new RuntimeException(exc);
            }
        }

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

    /**
     * Instantiates a new parallelized.
     * 
     * @param klass
     *            the klass
     * @throws Throwable
     *             the throwable
     */
    public Parallelized(Class<?> klass) throws Throwable {
        super(klass);
        setScheduler(new ThreadPoolScheduler());
    }
}

Below is the sample code, BaseSuite does not contain anything important. They are used with selenium, so it just installs webDriver. The getAllButOpera () method returns a collection of browser types that contain Internet Explorer, Firefox, and Chrome. These parameters are used to simultaneously run the same test on firefox, i.e. And chrome. I would like to run two tests in the class at the same time, which I am having problems with.

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Collection;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;


/**
 * The Class SampleSuite1.
 * 
 * @author Reid McPherson
 */
@RunWith(Parallelized.class)
public class SampleSuite1 {
    WebDriver driver;
    /**
     * Data.
     * 
     * @return the collection
     */
    @Parameters
    public static Collection<Object[]> data(){
          List<Object[]> browsers = new ArrayList<Object[]>();
    browsers.add(new String[]{"Firefox"});
    browsers.add(new String[]{"Chrome"});
    browsers.add(new String[]{"IE"});
    return browsers;
    }

    /**
     * Instantiates a new sample suite1.
     * 
     * @param type
     *            the type
     */
    public SampleSuite1(String type){
        switch (type) {
    case "FIREFOX":
        driver = new FirefoxDriver();
        break;
    case "IE":
        driver = new InternetExplorerDriver();
        break;
    case "CHROME":
        System.setProperty("webdriver.chrome.driver", PATHTOCHROMEEXE);
        driver = new ChromeDriver();
        break;
    case "OPERA":
        driver = new OperaDriver();
        break;
    default:
        throw new RuntimeException("Browser type unsupported");
    }
    // Set the timeout.
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    }

    /**
     * Sets the up.
     */
    @Before
    public void setUp() {
        driver.get("http://www.google.com");
    }

    /**
     * Test navigation succeeded.
     */
    @Test
    @TestDescription("Navigation Test")
    public void navigationShouldSucceed() {
        String pageSource = driver.getPageSource();
        assertTrue(pageSource.contains("Google"));
    }

    /**
     * Test title.
     */
    @Test
    @TestDescription("This method tests the web page title.")
    public void titleShouldBeGoogle() {
        assertEquals(driver.getTitle(), "Google");
    }

    @After
    public void finished(){
    driver.close();
    }


}
+3
source share
4 answers

, , , .

+1

, JUnit.

, :

Parallelized extends Parametrized extends Suite extends ParentRunner

:

ConcurrentTestRunner extends BlockJUnit4ClassRunner extends ParentRunner

, .

:

org.junit.runners.ParentRunner#getChildren

. org.junit.runners.BlockJUnit4ClassRunner :

protected List<FrameworkMethod> computeTestMethods() {
    return getTestClass().getAnnotatedMethods(Test.class);
}

. org.junit.runners.Parameterized :

for (int i= 0; i < parametersList.size(); i++)
  runners.add(newtestClassRunnerForParameters(getTestClass().getJavaClass(),
                parametersList, i));

.

: Parallelized org.junit.runners.ParentRunner#getChildren from BlockJUnit4ClassRunner.

+5

Jeeunit... 1.0 .

In ConcurrentRunnerScheduler, a ready-made method must be implemented.

So, I just pulled out the code and implemented it as the suiteFinished () method:

@Override
    public void finished() {
        try {
        while (!tasks.isEmpty())
            tasks.remove(completionService.take());
    }
    catch (InterruptedException e) {
        System.out.println("suite fin");
        Thread.currentThread().interrupt();
    }
    finally {
        while (!tasks.isEmpty())
            tasks.poll().cancel(true);
        executorService.shutdownNow();
    }

}
0
source

This is what I tried and it works great for me.

public class ParallelTests {
   static ExecutorService eService ;
   public static void main(String[] args) {
      testA() ;
      testB() ;
      testC() ;
   }
   public static void testA() {
      eService = Executors.newCachedThreadPool() ;
      for (int i = 0 ; i < 10 ; i++) {
         TestA testA = new TestA() ;
         eService.execute(testA) ;
      }
      eService.shutdown() ;
      while(!eService.isShutDown()) {
      }
   }
//same for testB and testC
}

public class TestA implements Runnable {
    public TestA() {
    }
    @Test
    public myTest throws Throwable {
    }
}
-1
source

All Articles