3

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

@Test
public void someTest1(){
}

@Test
public void someTest2(){
}

我可以让 someTest1() 同时运行所有参数,但是 someTest2() 仍然必须等待 someTest1() 在执行之前完成所有参数。我想知道是否有人知道能够同时运行带有所有参数的 someTest1() 和带有所有参数的 someTest2() 的解决方案?我尝试过tempus-fugit concurrent test runner,它非常适合未参数化的测试......

下面是我当前并行运行每个参数测试的代码。

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 不包含任何重要的内容。这些与 selenium 一起使用,因此它只设置 webDriver。getAllButOpera() 方法返回包含 Internet Explorer、Firefox 和 Chrome 的浏览器类型的集合。这些参数用于在firefox ie 和chrome 上同时运行相同的测试。我想在课堂上同时运行这两个测试,这是我遇到的问题。

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


}
4

4 回答 4

5

正如我所说,问题在于 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));

最后一个只提供课程。

建议:用org.junit.runners.ParentRunner#getChildren from BlockJUnit4ClassRunner.

于 2012-04-13T14:23:16.683 回答
1

感谢您的帮助,除了运行并发套件之外,我最终使用了这里的代码,它使我能够同时运行测试,但它不会同时运行相同的测试。

于 2012-04-17T16:17:31.490 回答
0

我也在使用 Jeeunit 的代码……但是即使是 1.0 版也有错误。

ConcurrentRunnerScheduler中,finished 方法必须被实现。

所以我只是拉取代码并像suiteFinished()方法一样实现它:

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

}
于 2016-09-29T14:53:06.573 回答
-1

这是我尝试过的,它对我来说非常有效。

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 {
    }
}
于 2012-04-20T03:33:24.700 回答