Ant生成的Junit报告为空

4

我正在尝试使用ant来运行junit测试并生成报告。我成功地运行了测试,但报告文件是空的。

我做错了什么?

这是我的build.xml:

<project name="JunitTest" default="test" basedir="."> 
    <property name="testdir" location="." /> 
    <property name="srcdir" location="." /> 
    <property name="full-compile" value="true" /> 
    <property name="test.reports" value="./reports" />

    <path id="classpath.base"/>
    <path id="classpath.test">

    <pathelement location="${testdir}" />
    <pathelement location="${srcdir}" />

    <path refid="classpath.base" />

    </path> 

    <target name="clean" >
        <delete verbose="${full-compile}"> 
            <fileset dir="${testdir}" includes="**/*.class" />
        </delete> `
    </target>

    <target name="compile" depends="clean">
        <javac srcdir="${srcdir}" destdir="${testdir}" verbose="${full-compile}" >
            <classpath refid="classpath.test"/>
        </javac>
    </target>

    <target name="test" depends="compile">
        <junit>
            <classpath refid="classpath.test" />
            <formatter type="brief" usefile="false" />
            <test name="com.tests.nav1" />
        </junit>

        <junitreport todir="${test.reports}">
            <fileset dir="${test.reports}">
                <include name="TEST-*.xml" />
            </fileset>
            <report todir="${test.reports}" />
        </junitreport>
    </target>

</project>

这是控制台上的输出:

[junit] Using CLASSPATH C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src;C:\jars\junit.jar;C:\ant\lib\ant-launcher.jar;C:\ant\lib\ant.jar;C:\ant\lib\ant-junit.jar;C:\ant\lib\ant-junit4.jar 
[junit] Testsuite: com.tests.nav1 
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 48.187 sec 
[junit] ------------- Standard Output --------------- 
[junit] testnav2 
[junit] ------------- ---------------- --------------- 
[junitreport] Using class org.apache.tools.ant.taskdefs.optional.TraXLiaison 
[junitreport] Processing C:\eclipse\eclipse-java-helios-SR1-win32\eclipse\JunitWS\SeleniumTraining\src\reports\TESTS-TestSuites.xml to C:\Users\pmahajan\AppData\Local\Temp\null236099757 
[junitreport] Loading stylesheet jar:file:/C:/ant/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl 
[junitreport] Transform time: 330ms 
[junitreport] Deleting: C:\Users\pmahajan\AppData\Local\Temp\null236099757 

BUILD SUCCESSFUL
Total time: 49 seconds
4个回答

7
如果您查看蚂蚁代码片段,存在一些问题:
  • 您设置了usefile=false,这意味着不会创建任何输出文件。
  • 您设置了formatter type=brief,它仅会为失败的测试打印详细信息。
  • 您还需要指定todir - 报告必须放在哪个文件夹中,位于<test>标签中 - 默认值为当前文件夹。这应该与您在<junitreport>任务中使用的文件夹匹配。
您可以尝试使用以下更新的<junit>部分...
    <junit>
        <classpath refid="classpath.test" />
        <formatter type="xml"/>
        <test name="com.tests.nav1" todir="${test.reports}"/>
    </junit>

2
   1 <target name="test" depends="compile">
   2     <junit>
   3         <classpath refid="classpath.test" />
   4         <formatter type="brief" usefile="false" />
   5         <test name="com.tests.nav1" />
   6     </junit>

   7     <junitreport todir="${test.reports}">
   8         <fileset dir="${test.reports}">
   9             <include name="TEST-*.xml" />
   10         </fileset>
   11         <report todir="${test.reports}" />
   12     </junitreport>
   13 </target>

您的代码段需要进行以下更改:
  1. 您需要在第5行指定目标目录选项(例如,todir = ${data.reports})。
  2. 在第8行中,指定的目录必须是data.reports。
  3. 第11行必须包含format选项,值为frames(format="frames")。

1

Ant JUnit任务doc提供了一个示例,可能会对您有所帮助(因为它显然可以实现您想要实现的功能):

<junit printsummary="yes" haltonfailure="yes">
    <classpath>
        <pathelement location="${build.tests}"/>
        <pathelement path="${java.class.path}"/>
    </classpath>

    <formatter type="plain"/>

    <test name="my.test.TestCase" haltonfailure="no" outfile="result">
        <formatter type="xml"/>
    </test>

    <batchtest fork="yes" todir="${reports.tests}">
        <fileset dir="${src.tests}">
            <include name="**/*Test*.java"/>
            <exclude name="**/AllTests.java"/>
        </fileset>
    </batchtest>
</junit>

在同一虚拟机中运行my.test.TestCase,忽略给定的CLASSPATH;如果此测试失败,仅打印警告。除了普通文本测试结果之外,此测试还将输出一个XML结果到result.xml。然后,在为${src.tests}定义的目录中每个匹配的文件都会在单独的虚拟机中运行一个测试。如果测试失败,则构建过程将中止。结果将收集在名为TEST-name.txt的文件中,并写入${reports.tests}。
该文档指定printsummary可以采用onoff两种值,但是在同一页上的示例中使用了yes,因此我想也被接受。

0
请尝试使用“xml”格式化程序。
   <formatter type="${junit.format}"/>

其中 junit.format 是具有适当值的属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接