本文介绍了在Maven 3下,surefire的forkMode是否'永远'不能与cobertura一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从maven 2切换到maven 3后,我发现cobertura报告的测试覆盖率为0%.我已经就使用哪个版本的cobertura和surefire提出了问题:

After switching from maven 2 to maven 3 I have found out having 0% test coverage reported by cobertura. I've stated question about which versions of cobertura and surefire to use: What versions of cobertura and surefire plugins work together under maven3?

但是,我已经对问题进行了更深入的调查,发现什么配置片段不起作用:

I have, however, investigated problem deeper and found out what of configuration fragment was not working:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${surefire.plugin.version}</version>
            <configuration>
                <forkMode>never</forkMode>
                <redirectTestOutputToFile>true</redirectTestOutputToFile>
                <argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
            </configuration>
        </plugin>

在将forkMode从never更改为once之后,开始生成测试覆盖率.因此,问题不是插件版本本身不兼容,而是在maven 3下由cobertura提供的surefire的各种fork模式支持的问题.

After changing forkMode from never to once the test coverage started to be generated. So, the problem was not the plugins version incompatibility itself, but the problem with the support by various fork modes of surefire by cobertura under maven 3.

所以,我的问题是,这是一个错误,还是cobertura插件的设计方式使其无法与某些forkMode=never一起使用?

So, my question is, is it a bug, or cobertura plugin is designed in such way, that it won't work with some forkMode=never?

推荐答案

Cobertura旨在在JVM退出时输出覆盖结果.

Cobertura is designed to output the coverage results when the JVM exits.

<forkMode>never</forkMode>指示Maven不要派生JVM来运行测试,而要重用当前的JVM.

<forkMode>never</forkMode> instructs Maven not to fork a JVM for running the tests but to re-use the current JVM.

在这种情况下,覆盖结果可能要等到Maven完成执行后才能输出.

In this case, the coverage results may not be output until Maven completes execution.

在Maven 2中,我不确定100%,但是我认为cobertura插件使用的派生生命周期(evilly)是如何导致派生生命周期的JVM fork或类加载器有效卸载的?结果相同.

In Maven 2, I am not 100% certain, but I think some of how the forked lifecycle (evilly) used by the cobertura plugin worked caused either a JVM fork for the forked lifecycle, or else a classloader unload effectively had the same result.

因此,在我看来这是Maven 2的一个错误,它的覆盖范围恰巧与<forkMode>never</forkMode>一起使用.

Thus in my opinion it was a bug of Maven 2 that coverage happened to work with <forkMode>never</forkMode>.

注意: <forkMode>never</forkMode>被认为是非常危险的,因为除其他问题外,每个类加载器的系统属性是如何划分范围的. <forkMode>once</forkMode>通常是最好的选择(除非您有滥用内存的测试-某些版本的JUnit会将所有测试类实例保留在内存中,直到在运行结束时报告为止,因此,如果每个测试类都保留在重对象上,则GC在测试运行结束之前将无法清除它们的存在.在这种情况下,将需要使用perclass/always forkMode)

Note: <forkMode>never</forkMode> is considered quite dangerous because of how System properties are not scoped per classloader, among other issues. <forkMode>once</forkMode> is generally the best option (unless you have tests that abuse memory - some versions of JUnit keep all the test class instances in memory until reporting at the end of the run, so if each test class holds on to heavy objects, GC will be unable to clear them out as they are live until the end of the test run. In such cases a perclass/always forkMode will be required)

这篇关于在Maven 3下,surefire的forkMode是否'永远'不能与cobertura一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 05:18