【问题标题】:Junit Coverage using Jacoco with ant build使用 Jacoco 和 ant build 的 Junit Coverage
【发布时间】:2015-04-20 08:58:02
【问题描述】:

我在使用 jacoco:coverage 执行 junit 以进行代码覆盖时遇到了这个特定问题。尝试了一些东西,但还没有成功。

我有这个 junit 构建脚本。

<target name="executeJunitMain" depends="createJunitLibs" description="Executes All Junit and prepare report.">

            <junit fork="yes" haltonfailure="off" failureProperty="junit.failure" includeantruntime="true" maxmemory="256m">
                <classpath refid="compile.class.path" />
                <formatter type="xml" />
                <jvmarg value="-javaagent:${external.junit.lib.dir}/jmockit.jar"/>
                <sysproperty key="jacoco-agent.destfile" file="${coverage.dir}/jacoco.exec"/>
                <batchtest fork="yes" todir="${report.dir}" >
                    <fileset dir="${dest.dir}">
                        <include name="**/Test*.class" />
                        <exclude name="**/AllTests.class" />
                        <exclude name="**/*$*.class"/>
                    </fileset>
                </batchtest>
            </junit>

        <antcall target="report"/>

    </target>

<!-- Execute the coverage report.-->
    <target name="report" description="Collect Coverage Report">

        <!-- Creates the Junit Report -->
        <junitreport todir="${report.dir}">
            <fileset dir="${report.dir}">
                <include name="TEST-*.xml"/>
            </fileset>
            <report format="frames" todir="${report.dir}/html"/>
        </junitreport>

        <!-- Creates the coverage Report.-->
        <mkdir dir="${report.dir}/coverage"/>
        <jacoco:report>
            <executiondata>
                <file file="${coverage.dir}/jacoco.exec" />
            </executiondata>

            <structure name="Code Coverage">
                <classfiles>
                    <zipfileset src="${sources.lib.dir}/${test.jar.name}.jar"/>
                                     </classfiles>
            </structure>
            <html destdir="${report.dir}/coverage" />
        </jacoco:report>

    </target>

使用这个 ant 脚本有时 junit 构建会失败。错误如下

BUILD FAILED
C:\Users\user\Codebases\junit.xml:111: The following error occurred while executing this line:
C:\Users\user\Codebases\junit.xml:147: The following error occurred while executing this line:
C:\Users\user\Codebases\junit.xml:164: Unable to read execution data file C:\Users\user\CodeBases\ju
nit\coverage\jacoco.exec

但有时它可以完美运行。我不确定它何时有效,何时无效。任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 我也正在尝试使用带有 jacoco 的 ant build 构建 android 单元测试,请您分享一下您的 build.xml。 Bcz 我是 jacoco 的新手。

标签: ant junit4 jacoco


【解决方案1】:

我找到了导致 jacoco 失败的问题。我在类路径中导入了一些检测的 jar,这些 jar 不是 junit 执行或覆盖所必需的。这些检测的 jar 导致 jacoco 会话不正确,因为我们也将 jmockit 集成为 Mocking Framework。以下链接对找出问题非常有帮助

eclemma.org/jacoco/trunk/doc/classids.html

【讨论】:

    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2018-12-08
    • 2015-06-24
    相关资源
    最近更新 更多