Cor*_*all 12 java testing junit
我想同时运行@Test跨多个类注释的每个方法.在某些情况下,我想限制这一点,并说每次只能运行100次.我希望带有@BeforeClass注释的方法在类中的任何Test运行之前运行一次,并且我希望@AfterClass在类运行中的所有测试之后运行一次注释.我想System.out,System.err和异常进行适当的缓冲/捕获,而不是写出来的,所以他们不交错,并且可以读取最终输出和明白发生了什么.
这存在吗?我有大量的独立测试用例,我的应用程序(我相信)线程安全.这些测试中没有一个具有JVM之外的依赖关系,并且我希望在给定硬件的情况下尽快完成它们.
如果这不存在,有没有具体的理由呢?全球junit用户丢失了多少时间,因为这并不容易?我可以把它构建成Junit吗?在我看来,这应该像一个标志一样简单,它"只是有效".
And*_*ert 18
你可以用JUnit来完成这个ParallelComputer(注意它仍然被认为是实验性的).这是一个非常简单的实现,由java.util.concurrent.ExecutorService API支持.
如果您对它的工作原理感到好奇,请查看源代码.
基本上,您为第一个参数调用JUnitCore.runClasses(Computer, Classes ...)并传入一个ParallelComputer对象.
用法示例:
import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.junit.runner.JUnitCore;
public class ParallelComputerExample {
@Test
public void runAllTests() {
Class<?>[] classes = { ParallelTest1.class, ParallelTest2.class };
// ParallelComputer(true,true) will run all classes and methods
// in parallel. (First arg for classes, second arg for methods)
JUnitCore.runClasses(new ParallelComputer(true, true), classes);
}
public static class ParallelTest1 {
@Test
public void test1a() {
lookBusy(3000);
}
@Test
public void test1b() {
lookBusy(3000);
}
}
public static class ParallelTest2 {
@Test
public void test2a() {
lookBusy(3000);
}
@Test
public void test2b() {
lookBusy(3000);
}
}
public static void lookBusy(long ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
System.out.println("interrupted");
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将在3秒内运行,因为所有方法和类都是并行运行的.
这将在6s内运行(因为所有类都是并行的).
JUnitCore.runClasses(new ParallelComputer(true, false), classes);
这也将在6s内运行(因为所有方法都是并行的).
JUnitCore.runClasses(new ParallelComputer(false, true), classes);
是的你可以。
如果您正在使用Maven。你可以帮忙
Maven-surefire-插件
在春天,
您可以检查此链接
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<parallel>classes</parallel>
<threadCount>5</threadCount>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
解决方案2:Junit4使用ParallelComputer提供并行功能
JUnit Toolbox为并行执行测试提供了 JUnit 运行器。
为了不混合输出到System.err和,System.out您必须在单独的 JVM 中开始测试,因为System.err和System.out是全局的。
| 归档时间: |
|
| 查看次数: |
32428 次 |
| 最近记录: |