与多线程并行运行多个测试

时间:2015-12-21 14:04:20

标签: java multithreading junit4

任何机构都可以说明我应该如何与多线程并行实现多个测试,这样我才能快速获得测试结果。目前,我的代码逐个执行测试,这会在测试时耗费大量时间。

  public class Test{

  @Test
  public void test1(){ }  

  @Test
  public void test2(){} 
  }

到目前为止,我已经关注了这个链接JUnit 4 TestRule to run a test in its own thread,我的测试可以在线程中执行但是一个接一个地执行我的目的。有什么建议??

3 个答案:

答案 0 :(得分:2)

如果使用maven,则可以配置maven构建以在单独的线程中运行每个方法。请在https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

查看更多信息

这样的事情应该有效:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <parallel>classesAndMethods</parallel>
      <threadCount>20</threadCount>
    </configuration>
</plugin>

答案 1 :(得分:0)

没有标准的方法只能使用JUnit。 您需要将此多线程运行器添加到项目中:https://gist.github.com/djangofan/4947053

之后,您可以通过向班级添加注释来并行运行所有测试:

@RunWith (MultiThreadedRunner.class)

答案 2 :(得分:0)

您可以通过JUnit满足您的要求,如其他答案所示。如果您有灵活选择其他测试框架,您可以尝试使用比JUnit更大的功能集的TestNG。从长远来看,这也可能有所帮助,值得一试。根据您的特定要求,您可以尝试使用以下链接与TestNG http://howtodoinjava.com/2014/12/02/testng-executing-parallel-tests/

并行运行测试
相关问题