Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ant中打印JUnit失败数_Ant_Junit - Fatal编程技术网

在Ant中打印JUnit失败数

在Ant中打印JUnit失败数,ant,junit,Ant,Junit,我想打印有多少测试用例: 失败 错误 通过 通过仅在build.xml文件中进行更改。如何才能做到这一点?您可以使用来收集测试结果 如果需要打印生成文件中的摘要度量,可以使用过滤器链从生成的报告中提取信息 可能(必须)有一个更简单的方法来做这件事,但我没有看到 <target name="test" depends="compile-test"> <junit failureProperty="test.failure"> <classpath

我想打印有多少测试用例:

  • 失败
  • 错误
  • 通过
  • 通过仅在build.xml文件中进行更改。如何才能做到这一点?

    您可以使用来收集测试结果

    如果需要打印生成文件中的摘要度量,可以使用过滤器链从生成的报告中提取信息

    可能(必须)有一个更简单的方法来做这件事,但我没有看到

    <target name="test" depends="compile-test">
        <junit failureProperty="test.failure">
          <classpath refid="classpath.test" />
    
          <formatter type="brief" usefile="false" />
          <batchtest>
            <fileset dir="${tst-dir}" includes="**/Test*.class" />
          </batchtest>
        </junit>
    
        <fail message="test failed" if="test.failure" />
      </target>
    

    如果您正在运行大量测试,您现在将有报告生成提取的开销。

    作为sudocodes方法的替代方法:您可以将printsummary属性设置为。这将在每个测试类之后打印摘要。这不是一个全面的总结

    Buildfile: C:\\test\unit_test.xml
    test:
        [junit] Test MyUnitTest FAILED
        [junit] Test MyUnitTest2 FAILED
    [junitreport] Processing C:\\test\tmp\TESTS-TestSuites.xml to C:\DOCUME~1\xxx\LOCALS~1\Temp\1\null1075123857
    [junitreport] Loading stylesheet jar:file:/C:/eclipse/eclipse-jee-ganymede-SR2-win32/eclipse/plugins/org.apache.ant_1.7.0.v200803061910/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
    [junitreport] Transform time: 906ms
    [junitreport] Deleting: C:\DOCUME~1\xxx\LOCALS~1\Temp\1\null1075123857
       [concat] Run: 8, Failed: 4, Errors: 1
    
    BUILD FAILED
    C:\test\unit_test.xml:32: test failed
    
    Total time: 1 second
    

    在junit任务中尝试此属性:

      <junit failureProperty="test.failure" printsummary="yes">
          <classpath refid="classpath.test" />
    
      <formatter type="brief" usefile="false" />
      <batchtest>
        <fileset dir="${tst-dir}" includes="**/Test*.class" />
      </batchtest>
    </junit>
    
    对于类似javadoc的html报告,请将格式化程序更改为:

    printsummary="yes"
    
    
    
    然后使用调用以下内容的目标创建报告:

    <formatter type="xml" />
    
    
    
    您可以使用从一组测试运行生成组合的XML报告

    要生成文本摘要,可以创建XSLT并使用ant目标格式化文件。这将生成一个输出文件,但您可以使用ant将其读入并回显到控制台

    XSLT应该使用类似的方法来统计测试用例、错误和失败

    <junitreport>
    <fileset dir="${report.dir}/tmp">
          <include name="TEST-*.xml" />
    </fileset>
    <report format="frames" styledir="${junitxslt.dir}" todir="${report.dir}/html" />
    </junitreport>
    

    (如果您真的只想修改ant构建文件,您可以在构建时将XSLT生成到临时文件夹,然后将其删除。)

    与sudocode的答案完全相同,但使用这一行来解析报告(适用于RSA 8.5.1/JUnit 4.11-我不允许将这段代码作为注释放在sudocodes代码中-我也不允许评论…):

    
    
    感谢sudocode!

    尝试嵌入到自定义任务中,并在构建中使用该任务从overview-summary.html中提取所需的数据

    下面是我的代码片段-

    import org.apache.tools.ant.BuildException;
    导入org.apache.tools.ant.Task;
    导入org.jsoup.jsoup;
    导入org.jsoup.nodes.Document;
    公共类ExtractJunitSummaryTask扩展任务{
    受保护字符串概述HTML;
    保护字符串输出属性;
    public void execute()引发BuildException{
    StringBuffer sb=新的StringBuffer();
    //TODO:读取概览摘要HTML文件内容并将其放入字符串中
    Document doc=Jsoup.parse(sb.toString());
    字符串allTests=doc.getElementsByAttributeValueContaining(“href”,“所有测试”).text();
    字符串allFailures=doc.getElementsByAttributeValueContaining(“href”,“alltests失败”).text();
    字符串allErrors=doc.getElementsByAttributeValueContaining(“href”,“alltests errors”).text();
    字符串allSkippedTests=doc.getElementsByAttributeValueContaining(“href”,“alltests skipped”).text();
    
    我先尝试了,但它会打印每个测试用例运行的摘要,而不是所有测试运行的总体摘要。
    <junitreport>
    <fileset dir="${report.dir}/tmp">
          <include name="TEST-*.xml" />
    </fileset>
    <report format="frames" styledir="${junitxslt.dir}" todir="${report.dir}/html" />
    </junitreport>
    
        count(//testsuites/testcase)
        count(//testsuites/testcase/error)
        count(//testsuites/testcase/error) 
    
    <replaceregex pattern='&lt;td&gt;&lt;a title="Display all tests" href="all-tests.html"&gt;(\d+)&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a title="Display all failures" href="alltests-fails.html"&gt;(\d+)&lt;/a&gt;&lt;/td&gt;&lt;td&gt;&lt;a title="Display all errors" href="alltests-errors.html"&gt;(\d+).*$' replace="Run: \1, Failed: \2, Errors: \3" />