在并行模式下使用maven-surefire-plugin时如何识别慢速单元测试? [英] How to identify slow unit tests when using maven-surefire-plugin in parallel mode?

查看:180
本文介绍了在并行模式下使用maven-surefire-plugin时如何识别慢速单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了管理/减少构建时间,我想确定哪些单元测试花费的时间最多 - 在并行测试环境中使用 maven-surefire-plugin

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.

我们使用 JUnit (4.10)进行单元测试。我们使用 maven (2.2.1 - 我们使用的一些插件还不支持maven 3)作为我们的主要构建工具,以及 maven-surefire -plugin (2.19)运行单元测试。

We are using JUnit (4.10) for unit tests. 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-surefire-plugin 并行模式,其中各个方法并行运行,单元测试类并行运行 - 这非常重要,因为它显着减少了构建单元的测试时间。 maven-surefire-plugin .pom 中配置如下:

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. The maven-surefire-plugin is configured in the .pom as follows:

  <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>

然而,其中一个含义是在控制台输出中,每个JUnit的时间已过去test class是类中所有方法的总时间。

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.

例如,如果测试类有10个单元测试方法,每个测试方法需要1秒才能运行,那么测试类需要大约1秒才能运行(每个方法并行运行),但输出类似于:

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:


运行com.package.QuickParallelTest测试运行:10,失败: 0,错误:
0,跳过:0,已过去时间:10.0秒 - 在com.package.QuickParallelTest

Running com.package.QuickParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.QuickParallelTest

这使用10个单元测试方法很难区分控制台输出与另一个测试类,其中9个几乎立即运行,1个运行差不多10秒。在这种情况下,测试类需要大约10秒才能运行(因为一个慢速测试方法),但 maven-surefire-plugin 控制台输出实际上是相同:

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. 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:


运行com.package.SlowParallelTest测试运行:10,失败:0,错误:
0,跳过:0 ,时间流逝:10.0秒 - 在com.package.SlowParallelTest

Running com.package.SlowParallelTest Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.0 sec - in com.package.SlowParallelTest

理想情况下,我希望时间过去表明测试需要多长时间class运行(并行),而不是单独运行方法所花费的总时间(就像单线程一样)。

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. 我缺少maven-surefire-plugin设置,因此打印摘要会显示每个课程所花费的时间方法的聚合?

  2. 这是 maven-surefire-plugin 中已知的错误(或功能) ? (我已经检查了 SureFire JIRA ,但找不到这样的内容。 )

  3. 我是否有另一种方法可以确定哪些测试需要很长时间,因此是优化的主要原因。

  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?
  2. Is this is a known "bug" (or "feature") in the maven-surefire-plugin? (I've checked the SureFire JIRA, but couldn't find anything like this.)
  3. Is there an alternative way for me to identify which tests are taking a long time and are therefore prime for optimisation.

编辑:

我尝试过玩一些额外的配置设置。奇怪的是,将以下内容添加到 .pom 中的配置似乎将控制台输出中经过的时间更改为运行测试类所花费的时间 - 但是,这是(在我看来)反直觉,因为这些设置是默认设置

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:

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


推荐答案

添加到Maven Surefire插件配置 reportFormat 条目并将其值设置为 plain (而不是默认的 brief )会给你每个方法的经过时间。

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.

<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>

默认reportFormat输出(简短):

Output with default 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

输出普通值:

-------------------------------------------------------
 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.

这篇关于在并行模式下使用maven-surefire-plugin时如何识别慢速单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆