【问题标题】:how to find the errors in junit classes while running jacoco in Ant build如何在 Ant build 中运行 jacoco 时查找 junit 类中的错误
【发布时间】:2020-06-13 09:00:31
【问题描述】:

我在我的 Ant 构建中使用 Jacoco 代码覆盖,并且在为我的 junit 类使用工具后构建成功,因为它们中有 powermockito。
当我运行 ant 并且 测试失败构建成功 时,我在 junit 类中遇到错误。 我正在使用来自另一个 build.xml 的 WAS 服务器 jar 编译 src 和测试类,并将类文件放在 dir.build 文件夹中。该文件夹作为仪器任务的输入,为我提供仪器类。当 junit 任务启动时,它表示测试类中发生错误并且测试失败。我使用了printsummary ="on" 并尝试使用详细生成日志。
我想知道运行junit时发生的错误是什么。有人可以告诉我如何查看测试类中的错误。

<?xml version="1.0" encoding="UTF-8"?>
<project name = "JunitIntegration" default = "report" xmlns:jacoco="antlib:org.jacoco.ant">

    <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
        <classpath path="./ant/lib/jacocoant.jar"/>
    </taskdef>


    <property name= "project.name" value = "Services"/>
    <property name= "source.dir" value = "C:\\Thejesh\\Workspaces\\firstSetup\\Services"/>
    <property name= "dir.build" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\build\\Services-classes"/>

    <!-- Code coverage report -->
    <property name= "result.dir" value = "C:\\Thejesh\\Workspaces\\edpm_firstSetup\\junitResults"/>
    <property name= "result.classes.instr.dir" value = "${result.dir}/classes-instr"/>
    <property name= "result.report.dir" value = "${result.dir}/site/jacoco"/>
    <property name= "result.exec.file" value = "${result.dir}/jacoco.exec"/>

    <import file="build-utils.xml"/>    

    <target name ="cleanL">
        <delete dir ="${result.dir}"/>
    </target>

    <target name ="createL">
        <mkdir dir ="${result.dir}"/>
        <mkdir dir ="${result.classes.instr.dir}"/>
    </target>



    <target name ="instrument" depends = "cleanL, createL">
        <jacoco:instrument destdir="${result.classes.instr.dir}">
            <fileset dir="${dir.build}"/>
        </jacoco:instrument>
    </target>


    <target name ="test" depends = "instrument">
        <record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="start"/>
        <jacoco:coverage destfile="${result.exec.file}" xmlns:jacoco="antlib:org.jacoco.ant"
            exclclassloader = "sun.reflect.DelegatingClassLoader">
            <junit fork="true" forkmode="once" printsummary="on">
                <formatter type= "xml"/>
                <classpath>
                    <pathelement path="${dir.build}" />
                    <pathelement path="./ant/lib/jacocoagent.jar" />
                    <fileset dir ="C:\\Thejesh\\PIF\\Jars\\Common">
                        <include name ="*.jar*" />
                    </fileset>
                </classpath> 

                <batchtest todir ="${result.dir}">
                    <fileset dir= "${result.classes.instr.dir}">
                        <include name ="**/*Test.class*"/>
                    </fileset>
                </batchtest>
            </junit>
        </jacoco:coverage>
        <record name="${result.dir}/loggerinfo.log" loglevel="verbose" action="stop"/>

    </target>

    <!-- Generating code coverage reports -->
    <target name ="report" depends ="test">
        <jacoco:report>
            <executiondata>
                <file file="${result.exec.file}"/>
            </executiondata>

            <structure name="JUnit intergration report">
                <classfiles>
                    <fileset dir="${dir.build}"/>
                </classfiles>
                <sourcefiles encoding="UTF-8">
                    <fileset dir="${source.dir}"/>
                </sourcefiles>
            </structure>

            <html destdir="${result.report.dir}"/>
            <xml destfile ="${result.report.dir}/report.xml"/>

        </jacoco:report>
    </target>

    <target name ="compiler" >
        <junit.compile
            projectdir="${source.dir}"
            destfile="${dir.build}"
        />
    </target>



</project>

【问题讨论】:

  • 这是 junit 还是 jacoco 的问题?如果注释掉jacoco标签,junit能正常工作吗?
  • 我认为是 Junit,因为我尝试通过评论 jacoco 标签来运行 junit,但仍然遇到同样的问题。
  • 您是否尝试过 junit 标签中的haltonfailure="true" 和haltonerror="true" 以使其尽早退出?
  • 它不是在错误出现时停止构建,我想在运行时找出错误是什么

标签: java junit ant jacoco powermockito


【解决方案1】:

一种可能对您有所帮助的方法。希望有人能提出一种更简单的方法——这是我几年前在升级到 JUNIT5 之前使用的 build.xml 中的 sn-p。

<junit printsummary="on" showoutput="off" fork="true"
      haltonerror="false" haltonfailure="false"
      errorproperty="junit.errors" failureproperty="junit.failures"  >
  <formatter type="plain"/>
  <formatter type="xml"/>

您需要在 ${result.dir} 中检查从 JUNIT4 获得的输出文件,并添加 ant 操作以抓取并打印出部分 junit 输出文件到控制台,以便最终的错误行可见

<loadfile srcfile="${junit.stdout}" property="junit.summary">
      <filterchain><tailfilter lines="30"/></filterchain>
</loadfile>

<echo>${junit.summary}</echo>

如果出现错误则退出构建:

<fail if="junit.failures">JUNIT failed - see ${result.dir}</fail>
<fail if="junit.errors"  >JUNIT error - see ${result.dir}</fail>

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 2017-04-19
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多