Determine the Execution Time of JUnit Tests – 确定JUnit测试的执行时间

最后修改: 2019年 11月 15日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Our builds often run a lot of automated test cases for our project. These include unit and integration tests. If the execution of the test suite takes a long time, we may wish to optimize our test code or track down tests that are taking too long.

我们的构建经常为我们的项目运行大量的自动化测试案例。这些包括单元和集成测试。如果测试套件的执行需要很长的时间,我们可能希望优化我们的测试代码或者追踪那些耗时过长的测试。

In this tutorial, we’ll learn a few ways to determine the execution time of our test cases and test suites.

在本教程中,我们将学习一些方法来确定我们的测试用例和测试套件的执行时间。

2. JUnit Examples

2.JUnit实例

To demonstrate reporting execution times, let’s use some example test cases from different layers of the test pyramid. We’ll simulate the test case duration with Thread.sleep().

为了演示报告执行时间,让我们使用一些来自测试金字塔不同层的测试用例。我们将用Thread.sleep()来模拟测试案例的持续时间。

We’ll implement our examples in JUnit 5. However, the equivalent tools and techniques also apply to test cases written with JUnit 4.

我们将在JUnit 5中实现我们的示例。然而,同等的工具和技术也适用于用JUnit 4编写的测试案例。

First, here’s a trivial unit test:

首先,这里有一个微不足道的单元测试。

@Test
void someUnitTest() {

    assertTrue(doSomething());
}

Second, let’s have an integration test that takes more time to execute:

第二,让我们有一个集成测试,需要更多的时间来执行。

@Test
void someIntegrationTest() throws Exception {

    Thread.sleep(5000);
    assertTrue(doSomething());
}

Finally, we can simulate a slow end-to-end user scenario:

最后,我们可以模拟一个缓慢的端到端用户场景。

@Test
void someEndToEndTest() throws Exception {

    Thread.sleep(10000);
    assertTrue(doSomething());
}

In the rest of the article, we’ll execute these test cases and determine their execution times.

在文章的其余部分,我们将执行这些测试案例并确定其执行时间

3. IDE JUnit Runner

3.IDE JUnit Runner

The quickest way to find the execution time of a JUnit test is to use our IDE. Since most IDEs come with embedded JUnit runner, they execute and report the test results.

找到JUnit测试的执行时间的最快速方法是使用我们的IDE。因为大多数IDE都带有嵌入式JUnit运行器,它们会执行并报告测试结果。

The two most popular IDEs, IntelliJ and Eclipse, have embedded JUnit runners.

两个最流行的IDE,IntelliJ和Eclipse,都有嵌入式的JUnit运行器。

2.1. IntelliJ JUnit Runner

2.1.IntelliJ JUnit运行器

IntelliJ allows us to execute JUnit test cases with the help of run/debug configurations. Once we execute the tests, the runner shows the test status along with the execution time:

IntelliJ允许我们在运行/调试配置的帮助下执行JUnit测试案例。一旦我们执行测试,运行器就会显示测试状态和执行时间。

Since we executed all three of our example test cases, we can see the total execution time as well as the time taken by each test case.

由于我们执行了所有三个测试用例,我们可以看到总执行时间以及每个测试用例所花费的时间

We may also need to save such reports for future reference. IntelliJ allows us to export this report in either HTML or XML format. The export report function is highlighted on the toolbar in the screenshot above.

我们可能还需要保存这样的报告供将来参考。IntelliJ允许我们以HTML或XML格式导出该报告。在上面的截图中,导出报告的功能在工具栏上被突出显示。

2.2. Eclipse JUnit Runner

2.2.Eclipse JUnit Runner

Eclipse also provides an embedded JUnit runner. We can execute and find out the execution time of a single test case or an entire test suite in the test results window:

Eclipse还提供了一个嵌入式JUnit运行器。我们可以在测试结果窗口中执行并找出单个测试案例或整个测试套件的执行时间。

But, in contrast to the IntelliJ test runner, we cannot export a report from Eclipse.

但是,与IntelliJ测试运行器相比,我们不能从Eclipse导出报告。

3. Maven Surefire Plugin

3.Maven Surefire Plugin

The Maven Surefire plugin is used to execute unit tests during the test phase of the build lifecycle. The surefire plugin is part of the default Maven configuration. However, if a specific version or additional configuration is required, we can declare it in the pom.xml:

Maven Surefire插件用于在构建生命周期的测试阶段执行单元测试。surefire插件是Maven默认配置的一部分。但是,如果需要特定的版本或额外的配置,我们可以在pom.xml中声明它。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
        <excludes>
            <exclude>**/*IntegrationTest.java</exclude>
        </excludes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
</plugin>

There are three ways to find the execution time of JUnit tests when testing with Maven. We’ll examine each one in the next subsections.

使用Maven进行测试时,有三种方法可以找到JUnit测试的执行时间。我们将在接下来的小节中逐一研究。

3.1. Maven Build Logs

3.1.Maven构建日志

Surefire displays the execution status and time of every test case in the build logs:

Surefire在构建日志中显示每个测试案例的执行状态和时间。

[INFO] Running com.baeldung.execution.time.SampleExecutionTimeUnitTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.003 s 
- in com.baeldung.execution.time.SampleExecutionTimeUnitTest

Here, it shows the combined execution time of all three test cases in the test class.

这里,它显示了测试类中所有三个测试案例的综合执行时间。

3.2. Surefire Test Reports

3.2.联信永益测试报告

The surefire plugin also generates a test execution summary in .txt and .xml formats. These are generally stored in the target directory of the project. Surefire follows a standard format for both text reports:

surefire插件还可以生成.txt和.xml格式的测试执行摘要。这些报告通常存储在项目的目标目录中。Surefire遵循两种文本报告的标准格式。

----------------------------------------------
Test set: com.baeldung.execution.time.SampleExecutionTimeUnitTest
----------------------------------------------
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 15.003 s 
- in com.baeldung.execution.time.SampleExecutionTimeUnitTest

and XML reports:

和XML报告。

<?xml version="1.0" encoding="UTF-8"?>
<testsuite
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation=
          "https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd"
	name="com.baeldung.execution.time.SampleExecutionTimeUnitTest"
	time="15.003" tests="3" errors="0" skipped="0" failures="0">
	<testcase name="someEndToEndTest"
		classname="com.baeldung.execution.time.SampleExecutionTimeUnitTest"
		time="9.996" />
	<testcase name="someIntegrationTest"
		classname="com.baeldung.execution.time.SampleExecutionTimeUnitTest"
		time="5.003" />
	<testcase name="someUnitTest"
		classname="com.baeldung.execution.time.SampleExecutionTimeUnitTest"
		time="0.002" />
</testsuite>

Although the text format is more suited for readability, the XML format is machine-readable and can be imported for visualization in HTML and other tools.

虽然文本格式更适合于可读性,但XML格式是机器可读的,可以导入HTML和其他工具中进行可视化

3.3. Surefire HTML Reports

3.3.可靠的HTML报告

We can view our test report in HTML in our browser by using the maven-surefire-report-plugin:

我们可以通过使用maven-surefire-report-plugin,在浏览器中用HTML查看我们的测试报告。

<reporting>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-report-plugin</artifactId>
            <version>3.0.0-M3</version>
        </plugin>
    </plugins>
</reporting>

We can now execute mvn commands to generate the report:

我们现在可以执行mvn命令来生成报告。

  1. mvn surefire-report:report – executes the tests and generates an HTML report
  2. mvn site – adds CSS styling to the HTML generated in the last step

This report shows the execution time of all the test cases in a class or a package along with the time taken by each test case.

该报告显示了一个类或包中所有测试用例的执行时间,以及每个测试用例所花费的时间。

4. Jenkins Test Results

4.詹金斯测试结果

If we are running CI in Jenkins, we can import the XML files written by surefire. This allows Jenkins to mark a build as failed if the tests fail, and to show us test trends and results.

如果我们在Jenkins中运行CI,我们可以导入由surefire编写的XML文件。这允许Jenkins在测试失败时将构建标记为失败,并向我们显示测试趋势和结果。

When we review the test results in Jenkins, we also see the execution times.

当我们在Jenkins中审查测试结果时,我们也会看到执行时间。

For our example, after installing Jenkins, we’ll configure a job using Maven and the surefire XML report files to publish the test results:

对于我们的例子,在安装完Jenkins后,我们将使用Maven和surefire XML报告文件配置一个作业,以发布测试结果。

We added a post-build action in our job to publish the test results. Jenkins will now import the XML files from the given path and add this report to the build execution summary:

我们在作业中添加了一个构建后的动作来发布测试结果。Jenkins现在将从给定的路径导入XML文件,并将此报告添加到构建执行摘要中。

This can also be achieved using Jenkins pipeline builds.

这也可以使用Jenkins pipeline构建来实现。

5. Conclusion

5.总结

In this article, we discussed various ways of determining the execution time of JUnit tests. The most immediate method is to use our IDE’s JUnit runner.

在这篇文章中,我们讨论了确定JUnit测试执行时间的各种方法。最直接的方法是使用我们IDE的JUnit运行器。

We then used the maven-surefire-plugin to archive the test reports in text, XML, and HTML formats.

然后我们使用maven-surefire-plugin将测试报告以文本、XML和HTML格式存档。

Finally, we provided our test report output to our CI server, which can help us analyze how different builds performed.

最后,我们向CI服务器提供了我们的测试报告输出,这可以帮助我们分析不同构建的表现。

As always, the example code from this article is available over on GitHub.

一如既往,本文中的示例代码可在GitHub上获得