mik*_*era 69 java concurrency junit multithreading unit-testing
我有一个大的JUnit测试套件,我非常希望同时运行所有测试,原因有两个:
我认识到这将迫使我重构一些代码以使其成为线程安全的,但我认为这是一件好事:-)
让JUnit同时运行所有测试的最佳方法是什么?
小智 31
你修复了JUnit吗?TestNG提供了开箱即用的良好多线程测试,并且它与JUnit测试兼容(您需要进行一些更改).例如,您可以运行如下测试:
@Test(threadPoolSize = 3, invocationCount = 9, timeOut = 10000)
public void doSomething() {
...
}
Run Code Online (Sandbox Code Playgroud)
这意味着该doSomething()方法将被3个不同的线程调用9次.
我强烈推荐TestNG.
Mik*_*lov 21
我正在寻找这个问题的答案,并根据这里的答案以及我在其他地方阅读的内容,看起来好像目前没有一种简单易用的方法来并行运行现有测试JUnit的.或者,如果有,我没有找到它.所以我写了一个简单的JUnit Runner来完成它.请随意使用; 有关MultiThreadedRunner类的完整说明和源代码,请参见http://falutin.net/2012/12/30/multithreaded-testing-with-junit/.使用此类,您只需注释现有的测试类,如下所示:
@RunWith(MultiThreadedRunner.class)
Run Code Online (Sandbox Code Playgroud)
Rom*_*ner 21
以下代码应该满足您的要求,这些要求来自德语书籍JUnit Profiwissen,其中包含一些并行测试内容或如何通过使用多个内核而不是仅使用单个内核来减少执行时间的提示.
JUnit 4.6引入了一个ParallelComputer类,它提供了并行执行的测试.但是,在JUnit 4.7提供为父运行器设置自定义调度程序的可能性之前,此功能无法公开访问.
public class ParallelScheduler implements RunnerScheduler {
private ExecutorService threadPool = Executors.newFixedThreadPool(
Runtime.getRuntime().availableProcessors());
@Override
public void schedule(Runnable childStatement) {
threadPool.submit(childStatement);
}
@Override
public void finished() {
try {
threadPool.shutdown();
threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Got interrupted", e);
}
}
}
public class ParallelRunner extends BlockJUnit4ClassRunner {
public ParallelRunner(Class<?> klass) throws InitializationError {
super(klass);
setScheduler(new ParallelScheduler());
}
}
Run Code Online (Sandbox Code Playgroud)
如果现在注释一个测试类,@RunWith(ParallelRunner.class)每个方法都将在自己的线程中运行.此外,执行机器上的CPU核心可用的活动线程数量(仅限).
如果要并行执行多个类,您可以定义一个自定义套件,如下所示:
public class ParallelSuite extends Suite {
public ParallelSuite(Class<?> klass, RunnerBuilder builder)
throws InitializationError {
super(klass, builder);
setScheduler(new ParallelScheduler());
}
}
Run Code Online (Sandbox Code Playgroud)
然后换@RunWith(Suite.class)用@RunWith(ParallelSuite.class)
您甚至可以WildcardPatternSuite通过直接从该套件扩展而不是Suite之前的示例来利用fe的功能.这使您可以通过任何方式过滤单元测试fe @Category- 只能UnitTest并行执行带注释类别的TestSuite 可能如下所示:
public interface UnitTest {
}
@RunWith(ParallelSuite.class)
@SuiteClasses("**/*Test.class")
@IncludeCategories(UnitTest.class)
public class UnitTestSuite {
}
Run Code Online (Sandbox Code Playgroud)
一个简单的测试用例现在看起来像这样:
@Category(UnitTest.class)
@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
@Test
public void testSomething() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
在UnitTestSuite将执行在与结束subdirecotries发现的每个类Test和具有@Category(UnitTest.class)并联指定-这取决于可用的CPU内核的数量.
我不确定它是否可以变得比那简单:)
显然,Mathieu Carbou为实现并发可能已经做了一些实现!
http://java.dzone.com/articles/concurrent-junit-tests
OneJunit
@RunWith(ConcurrentJunitRunner.class)
@Concurrent(threads = 6)
public final class ATest {
@Test public void test0() throws Throwable { printAndWait(); }
@Test public void test1() throws Throwable { printAndWait(); }
@Test public void test2() throws Throwable { printAndWait(); }
@Test public void test3() throws Throwable { printAndWait(); }
@Test public void test4() throws Throwable { printAndWait(); }
@Test public void test5() throws Throwable { printAndWait(); }
@Test public void test6() throws Throwable { printAndWait(); }
@Test public void test7() throws Throwable { printAndWait(); }
@Test public void test8() throws Throwable { printAndWait(); }
@Test public void test9() throws Throwable { printAndWait(); }
void printAndWait() throws Throwable {
int w = new Random().nextInt(1000);
System.out.println(String.format("[%s] %s %s %s",Thread.currentThread().getName(), getClass().getName(), new Throwable ().getStackTrace()[1].getMethodName(), w));
Thread.sleep(w);
}
}
Run Code Online (Sandbox Code Playgroud)
多个JUnit:
@RunWith(ConcurrentSuite.class)
@Suite.SuiteClasses({ATest.class, ATest2.class, ATest3.class})
public class MySuite {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
68184 次 |
| 最近记录: |