【问题标题】:Cobertura with Ant Script Junit test case failureCobertura 与 Ant Script Junit 测试用例失败
【发布时间】:2017-01-25 15:44:51
【问题描述】:

我有以下build.xml

  <?xml version="1.0" encoding="UTF-8"?
<project name="Sample" default="coverage" basedir=".">

    <property file="build.properties" />

    <path id="cobertura.classpath">
        <fileset dir="lib">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <target name="init">
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${instrumented.dir}" />
        <mkdir dir="${reports.xml.dir}" />
        <mkdir dir="${reports.html.dir}" />
        <mkdir dir="${coverage.xml.dir}" />
        <mkdir dir="${coverage.summaryxml.dir}" />
        <mkdir dir="${coverage.html.dir}" />
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes" includeantruntime="false">
            <classpath refid="cobertura.classpath" />
        </javac>

    </target>

    <target name="instrument" depends="init,compile">
            <!--
       Remove the coverage data file and any old instrumentation.
      -->
            <delete file="cobertura.ser" />
            <delete dir="${instrumented.dir}" />
            <!--
       Instrument the application classes, writing the
       instrumented classes into ${build.instrumented.dir}.
      -->
            <cobertura-instrument todir="${instrumented.dir}" datafile="cobertura.ser">
                <!--
        The following line causes instrument to ignore any
        source line containing a reference to log4j, for the
        purposes of coverage reporting.
       -->
                <ignore regex="org.apache.log4j.*" />
                <fileset dir="${classes.dir}">
                    <!--
         Instrument all the application classes, but
         don't instrument the test classes.
        -->
                    <include name="**/*.class" />
                    <exclude name="**/*Test.class" />
                </fileset>
            </cobertura-instrument>
        </target>

    <target name="test" depends="init,compile">
        <echo>${basedir}\cobertura.ser</echo>
        <junit fork="yes" dir="test" showoutput="yes" printsummary="yes" reloading="false">

             <sysproperty key="net.sourceforge.cobertura.datafile"
                    file="${basedir}\cobertura.ser" />
            <!--
                    Note the classpath order: instrumented classes are before the
                    original (uninstrumented) classes.  This is important.
            -->
            <classpath location="${instrumented.dir}" />
            <classpath location="${classes.dir}" />

            <!--
                    The instrumented classes reference classes used by the
                    Cobertura runtime, so Cobertura and its dependencies
                    must be on your classpath.
            -->
            <classpath refid="cobertura.classpath" />

            <formatter type="xml" />
            <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
            <batchtest todir="${reports.xml.dir}" unless="testcase">
                <fileset dir="test">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>

        <!-- JUnit Report in HTML -->
        <junitreport todir="${reports.xml.dir}">
            <fileset dir="${reports.xml.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${reports.html.dir}" />
        </junitreport>

    </target>

    <target name="coverage-check">
        <cobertura-check branchrate="34" totallinerate="100" />
    </target>

    <target name="coverage-report">
        <!--
                Generate an XML file containing the coverage data using
                the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
    </target>

    <target name="summary-coverage-report">
        <!--
                Generate an summary XML file containing the coverage data using
                the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
    </target>

    <target name="alternate-coverage-report">
        <!--
                Generate a series of HTML files containing the coverage
                data in a user-readable form using nested source filesets.
        -->
        <cobertura-report destdir="${coverage.html.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>
    </target>

    <target name="clean" description="Remove all files created by the build/test process.">
        <delete dir="${classes.dir}" />
        <delete dir="${instrumented.dir}" />
        <delete dir="${reports.dir}" />
        <delete file="cobertura.log" />
        <delete file="cobertura.ser" />
    </target>

    <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>
</project>

但是当我运行构建时,我得到 0% 的覆盖率。

直到instrument 目标我相信一切看起来都很好。但是当构建运行Test 目标时,测试失败了。不知道为什么会这样。如果我在ant 之外运行测试,它会成功。

有什么建议可以解决这个问题吗?

运行test 目标时出现以下错误

test:
    [junit] Running org.jtaddeus.playground.LogicTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.141 sec
    [junit] Test org.jtaddeus.playground.LogicTest FAILED
    [junit] Running org.jtaddeus.playground.ValidatorTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.14 sec
    [junit] Test org.jtaddeus.playground.ValidatorTest FAILED

【问题讨论】:

  • 只是为了确定:您没有使用 PowerMock?这有时会破坏您的覆盖率统计数据。
  • 我没用过PowerMock。我也想知道为什么我的测试用例会失败。我相信如果我的测试用例在构建中没有问题地执行,那么覆盖率也可能会出现在报告中。

标签: junit ant code-coverage cobertura


【解决方案1】:

终于找到了解决办法(不回答)

在跟踪 Junit 报告后,我发现它与 Junit 依赖有关。

我刚刚从构建路径中删除了 Junit-4 库,并将 Junit-4.7.jar 添加到构建路径中,你知道它的工作原理..

如果有人向我解释为什么会发生这种情况,我将非常感激。

如果我再次使用 Junit-4.11.jar,同样的失败。报告中的 Junit 错误为 error message="org/hamcrest/SelfDescribing" type="java.lang.NoClassDefFoundError"&gt;java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 2011-03-29
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多