【问题标题】:Running multiple test in parallel with Multithreading使用多线程并行运行多个测试
【发布时间】:2015-12-21 14:04:20
【问题描述】:

任何人都可以说明我应该如何使用多线程并行实现多个测试,以便我可以快速获得测试结果。目前我的代码一个一个地执行测试,这在测试时会浪费很多时间。

  public class Test{

  @Test
  public void test1(){ }  

  @Test
  public void test2(){} 
  }

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

【问题讨论】:

标签: java multithreading junit4


【解决方案1】:

如果您使用 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>

【讨论】:

    【解决方案2】:

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

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

    @RunWith (MultiThreadedRunner.class)
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        • 1970-01-01
        • 2011-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多