所以我尝试运行并行参数测试。 我有一个解决方案,其中相同的测试可以并行与例如提供的参数说我有以下运行:
@Test
public void someTest1(){
}
@Test
public void someTest2(){
}
我可以得到someTest1()与所有参数同时运行,但someTest2()将仍然执行之前等待someTest1()的所有参数来完成。 我想知道是否有人知道的解决方案,以便能够与所有参数和someTest2()与兼俱的参数运行someTest1()? 我试过的Tempus-福吉特并发测试运行 ,这对于未参数测试的伟大工程...
下面是代码,我对当前并行运行的每个参数的测试。
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,即相同的测试,和铬。 我想,在这我有什么麻烦的同时运行在类的两个测试。
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();
}
}