首页 > 解决方案 > 在 Maven 中使用 JUnit 类别运行测试时的性能损失

问题描述

我将一个项目从使用测试套件重构为使用 JUnit 类别注释。我注意到在使用类别时会出现相当显着的性能损失(慢 4 倍)。

背景:

开始分类测试:mvn test -P fast-testsrsp。mvn test -P smoke-tests(见下面的 pom.xml)。

Maven-输出

                          Old (suites)    New (FastTest)      new (SmokeTest)
Total time:               02:04 min        07:55 min          06:33 min
Time elapsed (summed up)  95.638 s         102.925 s          51.484 

使用类别的运行持续时间将近 4 倍(在执行相同数量的测试时)。执行冒烟测试(103 个测试用例)的持续时间几乎与执行所有 400 个测试用例的时间一样长。

显然也是从报道Total time到总结的大不同Time elapsed。后者似乎只是在测试方法内测量的时间,没有额外的开销(启动 JVM 等)。

看来 surfire 插件只需要内省 Category 注释就需要相当多的时间。

是否有可能克服这种性能损失?注意:由于被测试的生产代码需要初始化/完成一个全局库(可以访问本机库,这不能在同一系统上并行完成),所以我很想使用连续执行(没有分叉,没有并行等)。 )

Pom.xml(部分)

<profile>
    <id>smoke-tests</id>
    <properties>
        <test.includedGroups>
            ch.ergonomics.junit.category.SmokeTest
        </test.includedGroups>
        <test.excludedGroups></test.excludedGroups>
    </properties>
</profile>
<profile>
    <id>fast-tests</id>
    <properties>
        <test.includedGroups>
            ch.ergonomics.junit.category.FastTest
        </test.includedGroups>
        <test.excludedGroups></test.excludedGroups>
    </properties>
</profile>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <groups>${test.includedGroups}</groups>
                <excludedGroups>${test.excludedGroups}</excludedGroups>
                <useSystemClassLoader>false</useSystemClassLoader>
                <threadCount>1</threadCount>
                <forkCount>1</forkCount>
                <reuseForks>false</reuseForks>
            </configuration>
        </plugin>

冲浪插件版本:3.0.0-M5

标签: javajunitcategoriesmaven-surefire-plugintest-suite

解决方案


推荐阅读