如何让Sonar导出测试统计数据?

发布于 2024-12-01 19:40:49 字数 1507 浏览 3 评论 0 原文

我定义了以下 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 测试成功。

如何获取它以便深入了解每种测试方法及其统计数据?

I have the following Sonar Ant target defined:

<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>

When I run 'ant sonar' and bring up Sonar in my browser, I see info about the classes in the src directory, but nothing about the stuff in the test directory.

If I add ${test.src.dir} to sonar.sources and not set sonar.tests, I see some info about the test classes, but Sonar still reports 0 Test Successes.

How do I get it so I can drill down to each test method and their stats?

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

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

发布评论

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

评论(2

手心的温暖 2024-12-08 19:40:49

对于遇到此问题的其他人,我终于让 Sonar 报告了我们的 Emma Code 覆盖范围。第一个问题是 Emma 插件没有随我使用的 Sonar 版本(3.1.1)一起提供。我必须下载它并将其安装到extensions/ Sonar 的plugins 目录并重新启动它。

然后我必须在 build.xml 中设置以下属性:

<property name="sonar.core.codeCoveragePlugin" value="emma" />
<property name="sonar.emma.reportPath" value="${coverage.dir}" />

之后,运行 Sonar ant 任务后我至少看到以下输出:

[sonar:sonar] 13:41:49.705 WARN        org.sonar.INFO - No coverage (*.ec) file found in /my/local/path
[sonar:sonar] 13:41:49.708 WARN        org.sonar.INFO - No metadata (*.em) file found in /my/local/path

经过一番挖掘,我发现 Sonar Emma 插件,它是硬编码的,用于查找 .ec(覆盖范围)文件和 .em(元数据)文件。不幸的是,我的覆盖文件和元数据文件都有 .emma 扩展名,我无法重命名它们,因为这会破坏其他功能。因此,我编写了以下 Ant 任务来复制文件,以匹配 Sonar Emma 插件期望的命名标准。

<target name="createEmmaFilesWithSonarNamingStandard" depends="defineAntContribTasks">
    <if>
        <available file="${coverage.dir}/metadata.emma" />
        <then>
            <copyfile src="${coverage.dir}/metadata.emma" dest="${coverage.dir}/metadata.em" />
        </then>
    </if>
    <if>
        <available file="${coverage.dir}/coverage.emma" />
        <then>
            <copyfile src="${coverage.dir}/coverage.emma" dest="${coverage.dir}/coverage.ec" />
        </then>
    </if>
</target>

再次运行后,我遇到了一个新问题:

org.sonar.api.utils.SonarException: java.io.IOException: cannot read [/my/local/path/build/coverage/metadata.em]: created by another EMMA version [2.0.5312]

经过更多挖掘,我发现 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:

<property name="sonar.core.codeCoveragePlugin" value="emma" />
<property name="sonar.emma.reportPath" value="${coverage.dir}" />

After this, I atleast saw the following output after running the Sonar ant task:

[sonar:sonar] 13:41:49.705 WARN        org.sonar.INFO - No coverage (*.ec) file found in /my/local/path
[sonar:sonar] 13:41:49.708 WARN        org.sonar.INFO - No metadata (*.em) file found in /my/local/path

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.

<target name="createEmmaFilesWithSonarNamingStandard" depends="defineAntContribTasks">
    <if>
        <available file="${coverage.dir}/metadata.emma" />
        <then>
            <copyfile src="${coverage.dir}/metadata.emma" dest="${coverage.dir}/metadata.em" />
        </then>
    </if>
    <if>
        <available file="${coverage.dir}/coverage.emma" />
        <then>
            <copyfile src="${coverage.dir}/coverage.emma" dest="${coverage.dir}/coverage.ec" />
        </then>
    </if>
</target>

After running this again, I came across a new problem:

org.sonar.api.utils.SonarException: java.io.IOException: cannot read [/my/local/path/build/coverage/metadata.em]: created by another EMMA version [2.0.5312]

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.

樱娆 2024-12-08 19:40:49

属性“sonar.surefire.reportsPath”需要在定义声纳目标之前定义。

以下定义获取导出的测试信息(尽管它仍然没有导出覆盖率信息):

<property name='sonar.surefire.reportsPath' value='${test.dir}'/>

<property name='sonar.dynamicAnalysis' value='reuseReports'/>
<property name='sonar.emma.reportPath' value='${coverage.report.dir}'/>

<target name='sonar'>
    <property name='sonar.sources' value='${src.dir}'/>
    <property name='sonar.tests' value='${test.src.dir}'/>
    <property name='sonar.binaries' value='${build.dir}'/>
    <path id='jars'>
        <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
        <fileset dir='${ivy.lib.dir}/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'/>
</target>

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):

<property name='sonar.surefire.reportsPath' value='${test.dir}'/>

<property name='sonar.dynamicAnalysis' value='reuseReports'/>
<property name='sonar.emma.reportPath' value='${coverage.report.dir}'/>

<target name='sonar'>
    <property name='sonar.sources' value='${src.dir}'/>
    <property name='sonar.tests' value='${test.src.dir}'/>
    <property name='sonar.binaries' value='${build.dir}'/>
    <path id='jars'>
        <fileset dir='${env.JAVA_HOME}/jre/lib' includes='*.jar'/>
        <fileset dir='${ivy.lib.dir}/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'/>
</target>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文