如何获得 Maven 多模块项目的完整代码覆盖率
想象一个多模块 Maven 项目,如下所示:
parent
+- core
+- main
main
依赖于 core
模块。
我现在在 core
中编写一个类 CoreClass
,有 2 个方法:method1()
和 method2()
。 在core
测试中,我编写了一个仅测试CoreClass.method1()
的测试类。
如果我运行覆盖率工具(在我的例子中,Cobertura 使用 mvn sonar:sonar
),我会发现我在 CoreClass< 上获得了 50% 测试覆盖率/code> (如果我们假设这两种方法具有相同的长度)。
到现在为止,一切都还好。
现在,在 main
项目中,我编写了一个测试类来测试 CoreClass.method2()
。因此,通常,当我对整个项目进行分析时,我希望在 CoreClass
上获得 100% 的行覆盖率。
不过,我仍然得到了50%。
我明白这是一种综合行为。事实上,Cobertura 仅在 core
模块上执行测试时才会使用 CoreClass
进行覆盖率分析,而不是在 main 上代码>. 这解释了为什么我仍然有 50% 的代码覆盖率。
但是,我的问题是知道当我在所有模块上运行测试时是否有办法获得 CoreClass
的真实代码覆盖率。
谢谢!
ps:我知道在完美的世界中,main
模块不关心测试 core
类。但正如您所知,我们并不处于完美的世界中:o)
技术信息: Java 1.6、JUnit 4.8.1、Maven 2.0.9(很快就会升级到 2.2.1,但我不认为这真的很重要),声纳 2.8
Imagine a multi-modules Maven project, such as the following one:
parent
+- core
+- main
main
is dependent on the core
module.
I now write a class CoreClass
in core
, with 2 methods: method1()
and method2()
.
In core
tests, I write a test class that will only test CoreClass.method1()
.
If I run a coverage tool (in my case Cobertura, using mvn sonar:sonar
), I will find that I get 50% of test coverage on CoreClass
(if we imagine that both methods have the same length).
Until now, everything is ok.
Now, in main
project, I write a test class that will test the CoreClass.method2()
. So normally, I would expect to have 100% of line coverage on CoreClass
when I run an analysis on the whole project.
However, I still get my 50%.
I understand that this is a comprehensive behavior. Indeed, Cobertura will instrument CoreClass
for coverage analysis only during the tests execution on the core
module, and not on the main
.
That explains why I still have 50% of code coverage.
However, my question is to know if there is a way to get the real code coverage of CoreClass
when I am running the tests on all of my modules.
Thanks!
ps: I know that in a perfect world, it is not the concern of the main
module to test the core
classes. But as you may know, we are not in a perfect world :o)
Technical information: Java 1.6, JUnit 4.8.1, Maven 2.0.9 (will be upgraded to 2.2.1 soon, but I don't think it does really matter), Sonar 2.8
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 jacoco 和声纳,并为所有模块提供一个 jacoco.exec 文件结果。
声纳将使用此文件并报告每个模块的正确覆盖范围。
我已将它与 Sonar 一起成功用于多模块项目
Use jacoco and sonar and have a single jacoco.exec file result for all modules.
Sonar will use this file and report the correct coverage for each module.
I have use it for a multi module project successfully with Sonar
在这里您可以找到 jacoco/sonar 的解决方案,这里仅适用于 雅可可。
Here you can find a solution for jacoco/sonar and here only for jacoco.