【问题标题】:How to get Sonar to export test stats?如何让 Sonar 导出测试统计信息?
【发布时间】:2011-11-04 23:32:10
【问题描述】:

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

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

【问题讨论】:

    标签: ant junit sonarqube emma


    【解决方案1】:

    对于遇到此问题的其他人,我终于让 Sonar 报告我们的 Emma Code 覆盖率。第一个问题是我使用的 Sonar 版本(3.1.1)没有附带 Emma 插件。我必须download it并将其安装到Sonar的extensions/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 plugin 内部,查找 .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 plugin 页面所述。

    我下载了 Emma 的 2.1.5320 版本,替换了我的 Ant lib 目录中的 emma.jaremma_ant.jar。经过干净的重新编译和测试后,我能够重新运行 Sonar Ant 任务并将我的代码覆盖率反映在 Sonar 上。

    【讨论】:

      【解决方案2】:

      需要在定义声纳目标之前定义属性“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>
      

      【讨论】:

      • 您好,我有一个similar problem,但不太一样...你们是如何获得 .emma 文件的?
      • 我们实际上已经停止使用 Sonar 并将构建我们自己的构建数据存储库。
      猜你喜欢
      • 2011-01-30
      • 2017-12-23
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多