2011-12-02 129 views
4

我想用蚂蚁来运行junit测试并生成报告。 我能够成功运行测试,但报告文件为空。空的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 

回答

7

如果你看蚂蚁片段中,有几个问题:

  • 您已设置usefile=false,这意味着没有输出文件被创建
  • 您已设置formattertype=brief,将打印的详细信息仅用于失败的测试
  • 您还需要指定todir - 报告必须在<test>标记中显示的文件夹 - 默认为当前文件夹。这应该与您在<junitreport>任务中使用的文件夹匹配。

你可以用下面的更新<junit>节尝试...

<junit> 
     <classpath refid="classpath.test" /> 
     <formatter type="xml"/> 
     <test name="com.tests.nav1" todir="${test.reports}"/> 
    </junit> 
+0

非常感谢。这就像一个魅力。再次感谢。 –

1

蚂蚁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}定义的目录中的每个匹配文件,测试将在单独的VM中运行。如果测试失败,构建过程将中止。结果收集在名为TEST-name.txt的文件中,并写入$ {reports.tests}。

它在文档指定printsummary可取的值onoff,但他们在这是在同一页上的例子使用yes,所以我想这是公认的了。

0

请尝试用 “XML”

<formatter type="${junit.format}"/> 

其中junit.format是合适的值属性格式。

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日线({} data.reports例如todir = $)
  2. 在目录中指定要我data.reports 8号线的到目录选项。
  3. 第11行必须包含带值框的选项格式(format =“frames”)。