如何让Sonar导出测试统计数据?
我定义了以下 Sonar Ant 目标:
<target name='sonar'>
<property name='sonar.sources' value='${src.dir}'/>
<property name='sonar.tests' value='${test.src.dir}'/>
<property name='sonar.binaries' value='build/classes'/>
<path id='jars'>
<fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
<fileset dir='build/lib/test' includes='*.jar'/>
</path>
<pathconvert property='sonar.libraries' refid='jars' pathsep=','/>
<exec executable='p4' outputproperty='p4.P4CLIENT'>
<arg value='set'/>
<arg value='P4CLIENT'/>
</exec>
<propertyregex
property='p4client'
input='${p4.P4CLIENT}'
regexp='P4CLIENT=([^ ]+) *.*'
replace='\1'/>
<propertyregex
property='sonar.timestamp'
input='${build.time}'
regexp='_'
replace='T'/>
<sonar:sonar key='com.netflix:${module.name}' version='${p4client}@${sonar.timestamp}' xmlns:sonar='antlib:org.sonar.ant'/>
<property name='sonar.dynamicAnalysis' value='reuseReports'/>
<property name='sonar.emma.reportPath' value='${coverage.dir}'/>
</target>
当我运行“ant sonar”并在浏览器中启动 Sonar 时,我看到有关 src 目录中的类的信息,但没有看到有关 test 目录中的内容的信息。
如果我将 ${test.src.dir} 添加到 sonar.sources 并且未设置 sonar.tests,我会看到一些有关测试类的信息,但 Sonar 仍然报告 0 测试成功。
如何获取它以便深入了解每种测试方法及其统计数据?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于遇到此问题的其他人,我终于让 Sonar 报告了我们的 Emma Code 覆盖范围。第一个问题是 Emma 插件没有随我使用的 Sonar 版本(3.1.1)一起提供。我必须下载它并将其安装到
extensions/ Sonar 的plugins
目录并重新启动它。然后我必须在 build.xml 中设置以下属性:
之后,运行 Sonar ant 任务后我至少看到以下输出:
经过一番挖掘,我发现 Sonar Emma 插件,它是硬编码的,用于查找 .ec(覆盖范围)文件和 .em(元数据)文件。不幸的是,我的覆盖文件和元数据文件都有 .emma 扩展名,我无法重命名它们,因为这会破坏其他功能。因此,我编写了以下 Ant 任务来复制文件,以匹配 Sonar Emma 插件期望的命名标准。
再次运行后,我遇到了一个新问题:
经过更多挖掘,我发现 Sonar Emma 1.0.1 插件是针对 Emma 2.0.5312 编译的,而 Sonar Emma 1.1 和 1.2.x 是针对 Emma 版本 2.1.5320 编译的: Sonar Emma 上所述插件页面。
我下载了2.1.5320版本的Emma,也替换了 emma.jar作为emma_ant.jar 在我的 Ant lib 目录中。经过干净的重新编译和测试后,我能够重新运行 Sonar Ant 任务,并将我的代码覆盖率反映在 Sonar 上。
For anyone else that runs across this issue, I finally got Sonar to report on our Emma Code coverage. The first problem was that the Emma plugin did not come with the version of Sonar I was using (3.1.1). I had to download it and install it to the
extensions/plugins
directory of Sonar and restart it.Then I had to set the following properties in my build.xml:
After this, I atleast saw the following output after running the Sonar ant task:
After some digging, I found that inside of the Sonar Emma plugin, it is hard-coded to look for a .ec (coverage) file and a .em (metadata) file. Unfortunately, my coverage file had a .emma extension as did my metadata file and I was unable to rename them as it would break other functionality. So I wrote the following Ant task to copy the files to match the naming standard that the Sonar Emma plugin expects.
After running this again, I came across a new problem:
After some more digging, I found that the Sonar Emma 1.0.1 plugin was compiled against Emma 2.0.5312 and the Sonar Emma 1.1 and 1.2.x against Emma version 2.1.5320 as stated on the Sonar Emma plugin page.
I downloaded the 2.1.5320 version of Emma, replaced both emma.jar as well as emma_ant.jar in my Ant lib directory. After a clean re-compile and test, I was able to re-run the Sonar Ant task and have my code coverage reflected on Sonar.
属性“sonar.surefire.reportsPath”需要在定义声纳目标之前定义。
以下定义获取导出的测试信息(尽管它仍然没有导出覆盖率信息):
The property 'sonar.surefire.reportsPath' needs to be defined before the definition of the sonar target.
The following definition gets the test info exported (although it's still not exporting coverage info):