简体   繁体   English

在并行模式下使用maven-surefire-plugin时如何识别慢速单元测试?

[英]How to identify slow unit tests when using maven-surefire-plugin in parallel mode?

With a view to managing / reducing our build times, I want to identify which unit tests are taking the most time - in a parallel test environment using the maven-surefire-plugin . 为了管理/缩短构建时间,我想确定哪些单元测试花费的时间最多 - 在使用maven-surefire-plugin的并行测试环境中。

We are using JUnit (4.10) for unit tests. 我们使用JUnit (4.10)进行单元测试。 We use maven (2.2.1 - some plugins we use don't yet support maven 3) as our primary build tool, and the maven-surefire-plugin (2.19) to run unit tests. 我们使用maven (2.2.1 - 我们使用的一些插件还不支持maven 3)作为我们的主要构建工具,使用maven-surefire-plugin (2.19)来运行单元测试。

We are using the maven-surefire-plugin in parallel mode , where both the individual methods are run in parallel and the unit test classes are run in parallel - this is very important, as it significantly reduces build unit test time. 我们在并行模式下使用maven-surefire-plugin ,其中各个方法并行运行,单元测试类并行运行 - 这非常重要,因为它显着减少了构建单元的测试时间。 The maven-surefire-plugin is configured in the .pom as follows: maven-surefire-plugin .pom maven-surefire-plugin.pom配置如下:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <configuration>
      <argLine>-Xmx2G -XX:MaxPermSize=1G -XX:-UseSplitVerifier</argLine>
      <failIfNoTests>false</failIfNoTests>
      <parallel>classesAndMethods</parallel>
      <useUnlimitedThreads>true</useUnlimitedThreads>
    </configuration>
  </plugin>

However, one of the implications of this is that in the console output, the time elapsed for each JUnit test class is the aggregate time for all the methods in the class. 但是,其中一个含义是在控制台输出中,每个JUnit测试类所用的时间是该类中所有方法的总时间。

For example, if a test class had 10 unit test methods, each of which took 1 second to run, then the test class would take about 1 second to run (each method being run in parallel), but the output would be something like: 例如,如果一个测试类有10个单元测试方法,每个测试方法需要1秒才能运行,那么测试类需要大约1秒才能运行(每个方法并行运行),但输出结果如下:

Running com.package.QuickParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.QuickParallelTest 运行com.package.QuickParallelTest测试运行:10,失败:0,错误:0,跳过:0,已过去时间:10.0秒 - 在com.package.QuickParallelTest中

This makes it difficult to distinguish in the console output from another test class with 10 unit test methods, of which 9 run almost instantly and 1 takes almost 10 seconds to run. 这使得很难在控制台输出与另一个测试类中区分10个单元测试方法,其中9个几乎立即运行,1个运行几乎需要10秒。 In this case, the test class would take about 10 seconds to run (because of the one slow test method), but the maven-surefire-plugin console output would be effectively the same: 在这种情况下,测试类需要大约10秒才能运行(因为一个慢速测试方法),但是maven-surefire-plugin控制台输出实际上是相同的:

Running com.package.SlowParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.SlowParallelTest 运行com.package.SlowParallelTest测试运行:10,失败:0,错误:0,跳过:0,已过去时间:10.0秒 - 在com.package.SlowParallelTest

Ideally, I would like the time elapsed to indicate how long the test class took to run (in parallel), not the aggregate time taken to run the methods separately (as if single-threaded). 理想情况下,我希望花时间来指示测试类运行多长时间(并行),而不是单独运行方法所花费的总时间(就像单线程一样)。

So, my question(s) is/are: 所以,我的问题是:

  1. Is there maven-surefire-plugin setting that I am missing, so that the print summary would show time taken per class rather than aggregate for methods? 是否存在我缺少的maven-surefire-plugin设置,以便打印摘要显示每个类所花费的时间而不是方法的聚合?
  2. Is this is a known "bug" (or "feature") in the maven-surefire-plugin ? 这是maven-surefire-plugin已知的“错误”(或“功能”)吗? (I've checked the SureFire JIRA , but couldn't find anything like this.) (我检查了SureFire JIRA ,但找不到这样的东西。)
  3. Is there an alternative way for me to identify which tests are taking a long time and are therefore prime for optimisation. 是否有另一种方法可以确定哪些测试需要很长时间才能进行优化。

EDIT: 编辑:

I've tried playing with some additional configuration settings. 我试过玩一些额外的配置设置。 Curiously, adding the the following to the configuration in the .pom seems to change the time elapsed in the console output to be the time taken in running the test class - however, this is (in my mind) counter-intuitive, since these settings are the default settings : 奇怪的是,将以下内容添加到.pom的配置似乎将控制台输出中经过的时间更改为运行测试类所花费的时间 - 但是,这(在我看来)这是违反直觉的,因为这些设置是默认设置

    <configuration>
      ...
      <forkCount>1</forkCount>
      <reuseForks>true</reuseForks>
    </configuration>

Adding to the Maven Surefire Plugin configuration the reportFormat entry and setting its value to plain (instead of the default brief ) would give you elapsed time per method. 添加到Maven Surefire插件配置的reportFormat条目并将其值设置为plain (而不是默认的brief )将为每个方法提供经过的时间。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <argLine>-Xmx2G -XX:MaxPermSize=1G -XX:-UseSplitVerifier</argLine>
                <failIfNoTests>false</failIfNoTests>
                <parallel>classesAndMethods</parallel>
                <useUnlimitedThreads>true</useUnlimitedThreads>
                <reportFormat>plain</reportFormat>
            </configuration>
        </plugin>
    </plugins>
</build>

Output with default reportFormat ( brief ): 使用默认reportFormat输出( brief ):

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sample.mocking.InternalServiceTestCase
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.241 sec - in com.sample.mocking.InternalServiceTestCase

Output with plain value: 具有plain价值的输出:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sample.mocking.InternalServiceTestCase
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.187 sec - in com.sample.mocking.InternalServiceTestCase
test(com.sample.mocking.InternalServiceTestCase)  Time elapsed: 0.005 sec
mockTest(com.sample.mocking.InternalServiceTestCase)  Time elapsed: 0.17 sec
mockTestFailureTollerance(com.sample.mocking.InternalServiceTestCase)  Time elapsed: 0.007 sec
mockProcessfile(com.sample.mocking.InternalServiceTestCase)  Time elapsed: 0.003 sec

This option may give you further details on tests and execution time. 此选项可为您提供有关测试和执行时间的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven-surefire插件和分叉模式 - Maven-surefire-plugin and forked mode 如何使用路径通过 maven-surefire-plugin 包含集成测试 - How to include integration tests via maven-surefire-plugin using path 如何使用maven surefire插件并行执行testng测试 - how to execute testng tests in parallel using maven surefire plugin 如何使 maven-surefire-plugin 重用创建的线程来执行所有 Maven 模块中的所有测试 - How to make maven-surefire-plugin reuse created thread to execute all tests in all Maven modules 如何在控制台上显示maven-surefire-plugin单元测试覆盖率报告 - How to show maven-surefire-plugin unit test coverage report on console 带有Maven-surefire-plugin的UnsatisfiedLinkError - UnsatisfiedLinkError with maven-surefire-plugin 如何使用在docker中运行的mysql运行maven测试(使用maven-surefire-plugin) - How to run maven test with mysql running in docker (using maven-surefire-plugin) 与 maven surefire 插件并行运行测试 - Running tests in parallel with maven surefire plugin 运行 maven-surefire-plugin:test 时出现 lombok 编译错误 - lombok compilation errors when running maven-surefire-plugin:test 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 一起工作 - How to make maven-surefire-plugin work with TestNG in Eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM