当前位置: 首页 > 面试题库 >

无法将JUnit测试作为ant目标执行

唐元青
2023-03-14
问题内容

我正在尝试使用针对Jenkins的Cobertura插件获得代码覆盖率,因此我在build.xml中进行检测,运行测试,然后进行覆盖率报告,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<project name="CoberturaAndJenkins" default="default" basedir=".">
    <description>Builds, tests, and runs the project CoberturaAndJenkins.</description>
    <import file="nbproject/build-impl.xml"/>

    <property name="lib.dir" value="lib/" />
    <property name="junit.file" value="junit-4.11.jar" />
    <property name="cobertura.dir" value="${lib.dir}/cobertura/" />
    <property name="instrumented.dir" value="/var/lib/jenkins/workspace/CoberturaAndJenkins/instrumented/" />
    <property name="coveragereport.dir" value="build/cobertura/" />
    <property name="classes.dir" value="/var/lib/jenkins/workspace/CoberturaAndJenkins/build/classes/" />
    <property name="reports.xml.dir" value="build/test/results/" />

    <!-- Modules that build -->
    <property name="src.dir" value="src/" />
    <property name="test.dir" value="test/" />


    <!-- Define the Cobertura Ant tasks -->
    <path id="cobertura.classpath">
        <fileset dir="${cobertura.dir}">
            <include name="cobertura-2.0.3.jar" />
            <include name="lib/**/*.jar" />
        </fileset>
        <fileset dir="${lib.dir}" >
            <include name="${junit.file}" />
        </fileset>
    </path>

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

    <path id="test.classpath">
        <path refid="cobertura.classpath" />
        <pathelement location="${bin}" />
        <pathelement location="${instrumented.dir}"/>
        <pathelement location="${classes.dir}" />
    </path>

    <!--============= Instrument the classes that JUnit will be testing =============-->
    <target name="instrument">
        <delete file="cobertura.ser"/>
        <delete dir="${instrumented.dir}" />
        <cobertura-instrument todir="${instrumented.dir}" datafile="cobertura.ser" >
            <fileset dir="${classes.dir}">
                <include name="**/*.class" />
                <exclude name="**/*Test.class" />
            </fileset>
        </cobertura-instrument>
    </target>

    <!--======================= JUNIT =======================-->

   <property name="basedir" value="./" />
    <target name="module-test" depends="instrument"> 
        <echo level="info" message="Running test cases..." />

        <junit dir="${basedir}" showoutput="yes" fork="yes" printsummary="yes" failureProperty="test.failed">
            <!--Specify the name of the coverage data file to use.
                    The value specified below is the default.-->
            <sysproperty key="net.sourceforge.cobertura.datafile" file="cobertura.ser" />

            <classpath refid="test.classpath" />

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


    <!--===================== COBERTURA REPORT ================================-->
    <target name="coverage-report" depends="module-test">
        <cobertura-report format="xml" destdir="${coveragereport.dir}" datafile="cobertura.ser" >
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </cobertura-report>
        <cobertura-report format="html" destdir="${coveragereport.dir}" datafile="cobertura.ser" >
            <fileset dir="${src.dir}">
                <include name="**/*.java" />
            </fileset>
        </cobertura-report>
    </target>
    <!--================== END OF COBERTURA REPORT ============================-->

</project>

事实是,当它尝试运行单元测试(我可以使用运行ant test)时,它总是在说

module-test:
Running test cases...
Running coberturaandjenkins.CoberturaAndJenkinsTest
Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
Test coberturaandjenkins.CoberturaAndJenkinsTest FAILED

我已经更改,重新更改,重新更改了build.xml,好几天都没有运气了。我无法正常工作。拜托,我会很感谢您的帮助。这是我第一次使用蚂蚁(和Cobertura)


问题答案:

谢谢大家的回答。经过进一步调查,我弄清楚了如何使其工作。我必须完全重新编写build.xml。这里是:

<property file="build.properties" />

<path id="cobertura.classpath">
    <fileset dir="${cobertura.dir}">
        <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>
    <javac srcdir="${test.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}">
        <!--
                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">
    <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
        <!--
                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.src.dir}">
                <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."/>

属性定义在开头引用的build.properties文件中



 类似资料:
  • 这是我添加的公共类NewEmptyJUnitTest的代码{ 这是pom.xml文件 我一直收到这个错误:

  • 问题内容: 这是我的ANT JUnit目标 如果通过Eclipse运行,我的单元测试可以通过,但是如果我通过ANT对其进行洗衣,则单元测试将失败。我希望它在单元测试的中断点停止。从文档中我知道我需要添加这些jvmarg,但是无法使其停止,因此显然我没有将它们放在正确的位置。另外,我认为端口不正确,但是应该使用哪个端口?通过Eclipse调试JUnit时,我不必设置任何调试端口,它可以正常工作 问题

  • 我有一个名为dealDeck()的公共静态void方法,该方法从名为deck的ArrayList中获取对象,并根据它们在列表中的位置将它们分离并放置到4个不同的ArrayList中。是否有JUnit测试来检查方法是否按计划运行? 是否可以或应该测试此方法?

  • 我有一个gradle任务,它有一个dependsOn,然后需要执行一个名为runcukes的Ant任务。然而,Gradle不会调用ant任务: 使用-d运行gradle会显示gradle无法识别Ant目标runcukes:

  • 有关错误和可能的解决方案的更多信息,请阅读以下文章:http://cwiki.apache.org/confluence/display/maven/mojoExecutionException

  • 当其中一个测试在我的maven项目中失败时,我收到以下错误:未能在项目java上执行目标org.codehaus.mojo:exec-maven-plugin:1.6.0:exec(默认值):命令执行失败。:进程退出,错误:1(退出值:1)- 我正在尝试使用Hiptest和TravisCI自动化我的测试。我从Hiptest分叉了hps-cucumber-java项目,并遵循了指南。当所有测试通过时