3

だから私は並列パラメータ化されたテストを実行しようとしています。同じテストを提供されたパラメーターと並行して実行できるソリューションがあります。たとえば、次のようなものがあります。

@Test
public void someTest1(){
}

@Test
public void someTest2(){
}

someTest1()をすべてのパラメーターで同時に実行することはできますが、someTest2()は、実行する前にsomeTest1()がすべてのパラメーターで完了するのを待つ必要があります。someTest1()をすべてのパラメーターで実行し、someTest2()をすべてのパラメーターで同時に実行できるソリューションを誰かが知っているかどうか疑問に思いましたか?私はtempus-fugit同時テストランナーを試しました。これは、パラメーター化されていないテストに最適です...

以下は、現在各パラメーターテストを並行して実行するために持っているコードです。

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で同じテストを同時に実行するために使用されます。クラスで2つのテストを同時に実行したいのですが、これが問題になっています。

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 に答える