带参数的并发JUnit测试 [英] Concurrent JUnit Tests with Parameters

查看:107
本文介绍了带参数的并发JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试运行并行参数化测试.我有一个解决方案,其中相同的测试可以与提供的参数并行运行,例如说我有以下内容:

So I'm attempting to run parallel parameterized tests. I have a solution where the same test can run in parallel with the parameters supplied for example say I have the following:

@Test
public void someTest1(){
}

@Test
public void someTest2(){
}

我可以使someTest1()同时与所有参数一起运行,但是someTest2()在执行之前仍必须等待someTest1()与所有参数一起完成.我想知道是否有人知道一种能够同时运行所有参数的someTest1()和同时运行所有参数的someTest2()的解决方案?我已经尝试过 tempus-fugit并发测试运行器,它对于未参数化的测试非常有用...

I can get someTest1() to run with all the parameters concurrently, but someTest2() will have still have to wait for someTest1() to complete with all parameters before executing. I was wondering if anyone knew of a solution to be able to run someTest1() with all parameters and someTest2() with all parameters concurrently? I've tried tempus-fugit concurrent test runner, which works great for tests that are not parameterized...

下面是我目前用于并行运行每个参数测试的代码.

Below is code that I have for currently running each parameter test 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());
    }
}

下面的代码是一个示例测试,BaseSuite不包含任何非常重要的内容.这些与硒一起使用,因此只设置了webDriver. getAllButOpera()方法返回包含Internet Explorer,Firefox和Chrome的浏览器类型的集合.这些参数用于在Firefox和chrome上同时运行相同的测试.我想同时在类中运行两个测试,这是我遇到的麻烦.

The code below is an example test, BaseSuite doesn't contain anything of much importance. These are being used with selenium so it just sets the webDriver. The getAllButOpera() method returns a collection of browser types that contain Internet Explorer, Firefox, and Chrome. These parameters are used to run the same test on firefox, ie, and chrome concurrently. I would like to run the two tests in the class at the same time which is what I am having trouble 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();
    }


}

推荐答案

感谢您的帮助,我最终使用了

Thanks for the help, I ended up using code from here in addition to running concurrent suites and it gave me the ability to run the tests simultaneously, but it will not run the same test at the same time.

这篇关于带参数的并发JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆