Cobertura 的单元测试速度很慢

发布于 2024-12-06 11:18:49 字数 2986 浏览 1 评论 0原文

我最近将 Cobertura 集成到我的 Ant 构建脚本中,我想知道我是否做得正确,因为它显着减慢了运行单元测试所需的时间。

这是一个示例控制台输出:

...
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.ViewportDeterminingMarkupStrategyTest
[junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0.38 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.VisibleFeatureTypesMarkupInfoTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.434 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.basemap.BasemapByViewportStrategyTest
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.016 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.basemap.BasemapByZoomLevelAndCenterPointStrategyTest
[junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 1.853 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
...

每次测试运行后 Cobertura 都会说:

[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
...

这是我的 Ant 构建脚本中的单元测试任务:

    <target name="unit-test" depends="compile-unit-test">
    <delete dir="${reports.xml.dir}" />
    <delete dir="${reports.html.dir}" />
    <mkdir dir="${reports.xml.dir}" />
    <mkdir dir="${reports.html.dir}" />

    <junit fork="yes" dir="${basedir}" failureProperty="test.failed" printsummary="on">
        <!--
                Note the classpath order: instrumented classes are before the
                original (uninstrumented) classes.  This is important.
            -->
        <classpath location="${instrumented.dir}" />
        <classpath refid="test-classpath" />

        <formatter type="xml" />
        <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
        <batchtest todir="${reports.xml.dir}" unless="testcase">
            <fileset dir="TestSource">
                <include name="**/*Test.java" />
                <exclude name="**/XmlTest.java" />
                <exclude name="**/ElectedOfficialTest.java" />
                <exclude name="**/ThematicManagerFixturesTest.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

我的设置和输出看起来正确吗?单元测试单独运行时需要 2.234 秒,而在使用 Cobertura 的构建脚本中运行时需要 3 分钟,这是否正常?

I recently integrated Cobertura into my Ant build scripts and I am wondering if I did it correctly because it has significantly slowed down the time it takes to run the unit tests.

Here is a sample console output:

...
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.ViewportDeterminingMarkupStrategyTest
[junit] Tests run: 5, Failures: 0, Errors: 0, Time elapsed: 0.38 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.VisibleFeatureTypesMarkupInfoTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.434 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.basemap.BasemapByViewportStrategyTest
[junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.016 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
[junit] Running gov.nyc.doitt.gis.webmap.strategy.markup.basemap.BasemapByZoomLevelAndCenterPointStrategyTest
[junit] Tests run: 4, Failures: 0, Errors: 0, Time elapsed: 1.853 sec
[junit] Flushing results...
[junit] Flushing results done
[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
...

It seems fishy that after every test run Cobertura says:

[junit] Cobertura: Loaded information on 282 classes.
[junit] Cobertura: Saved information on 282 classes.
...

Here is my unit test task from my Ant build script:

    <target name="unit-test" depends="compile-unit-test">
    <delete dir="${reports.xml.dir}" />
    <delete dir="${reports.html.dir}" />
    <mkdir dir="${reports.xml.dir}" />
    <mkdir dir="${reports.html.dir}" />

    <junit fork="yes" dir="${basedir}" failureProperty="test.failed" printsummary="on">
        <!--
                Note the classpath order: instrumented classes are before the
                original (uninstrumented) classes.  This is important.
            -->
        <classpath location="${instrumented.dir}" />
        <classpath refid="test-classpath" />

        <formatter type="xml" />
        <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
        <batchtest todir="${reports.xml.dir}" unless="testcase">
            <fileset dir="TestSource">
                <include name="**/*Test.java" />
                <exclude name="**/XmlTest.java" />
                <exclude name="**/ElectedOfficialTest.java" />
                <exclude name="**/ThematicManagerFixturesTest.java" />
            </fileset>
        </batchtest>
    </junit>
</target>

Does my setup and output seem correct? Is it normal for the unit tests to take 2.234 seconds when run alone and when run in the build script with Cobertura take 3 minutes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-12-13 11:18:49

来自 cobertura-anttask 参考

出于同样的原因,如果您使用 ant 1.6.2 或更高版本,那么您
可能想要设置 forkmode="once" 这将导致只有一个 JVM
启动所有 JUnit 测试,并将减少
Cobertura 每次 JVM 都会读取/写入覆盖率数据文件
开始/停止。

(重点是我的。)

From cobertura-anttask reference:

For this same reason, if you're using ant 1.6.2 or higher then you
might want to set forkmode="once" This will cause only one JVM to be
started for all your JUnit tests, and will reduce the overhead of
Cobertura reading/writing the coverage data file each time a JVM
starts/stops.

(Emphasis is mine.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文