使用Cobertura进行单元测试速度缓慢

8

我最近将 Cobertura 集成到我的 Ant 构建脚本中,但我发现运行单元测试的时间明显变慢了。我想知道是否正确地集成了 Cobertura。

以下是一段示例控制台输出:

...
[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分钟,这是否正常?


我不认为有什么问题(除了使用Ant :-P),但这种延迟绝对不正常。 - Nathan Hughes
我猜你除了Ant之外还有其他首选的工具?你会推荐什么? - Sarah Haskins
Maven和Gradle都比Ant有了很大的改进。在Maven项目中添加Cobertura非常简单。 - Nathan Hughes
1个回答

8
来自cobertura-anttask参考
由于这个原因,如果你使用的是ant 1.6.2或更高版本,则可能需要设置forkmode =“once”。这将仅为所有JUnit测试启动一个JVM,并减少每次JVM启动/停止时Cobertura读取/写入覆盖数据文件的开销。
(强调是我的。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接