在xml中自动生成JUnit结果的最佳方法是什么?

我目前正在JUnit中编写一系列测试。 我需要自动将结果导出为XML。 我在读这样做的最好方法是通过扩展RunListener类并以这种方式编写XML。 下面是我迄今为止所做的一些示例,但我正在努力解决已经运行的每个测试的信息。 '描述'类似乎没有任何有用的获取方法,我觉得我可能会这样做是错误的。

有人可以协助如何从描述中获得有用的信息(例如,测试通过/失败,测试持续时间,测试名称等)还是给我提供一些我应该做什么的建议?

public class XmlListener extends RunListener {

    private final PrintStream fWriter;

    public XmlListener(JUnitSystem system) {
        this(system.out());
    }

    public XmlListener(PrintStream writer) {
        this.fWriter = writer;
    }

    @Override
    public void testRunStarted(Description description) {
        fWriter.append("<?xml version="1.0" encoding="UTF-8"?>n");
    }

    @Override
    public void testRunFinished(Result result) {
        fWriter.append("tt</suite>n");
        fWriter.append("t</suites>n");
        fWriter.append("</result>n");
        printHeader(result.getRunTime());
        printFailures(result);
        printFooter(result);

    }

    @Override
    public void testStarted(Description description) {
        fWriter.append("ttt<case>n");
        fWriter.append("tttt<timestamp>"  + "</timestamp>n");
        fWriter.append("tttt<className>"  + "</className>n");
        fWriter.append("tttt<testName>"  + "</testName>n");
    }

    @Override
    public void testFinished(Description description) {
        fWriter.append("tttt<duration>"  + "</duration>n");
        fWriter.append("ttt</case>n");
        Iterator it = description.getAnnotations().iterator();
        while (it.hasNext()) {
            fWriter.append(it.next().toString());
        }
    }

    @Override
    public void testFailure(Failure failure) {
        fWriter.append('E');
    }

    @Override
    public void testIgnored(Description description) {
        fWriter.append('I');
    }

    /**
     * private methods
     * @return
     */

    private PrintStream getWriter() {
        return fWriter;
    }

    protected void printHeader(long runTime) {
        getWriter().println();
        getWriter().println("Time: " + elapsedTimeAsString(runTime));
    }

    protected void printFailures(Result result) {
        List<Failure> failures= result.getFailures();
        if (failures.size() == 0)
            return;
        if (failures.size() == 1)
            getWriter().println("There was " + failures.size() + " failure:");
        else
            getWriter().println("There were " + failures.size() + " failures:");
        int i= 1;
        for (Failure each : failures)
            printFailure(each, "" + i++);
    }

    protected void printFailure(Failure each, String prefix) {
        getWriter().println(prefix + ") " + each.getTestHeader());
        getWriter().print(each.getTrace());
    }

    protected void printFooter(Result result) {
        if (result.wasSuccessful()) {
            getWriter().println();
            getWriter().println("t</suites>n");
            getWriter().println("</result>n");
            getWriter().println(" (" + result.getRunCount() + " test" + (result.getRunCount() == 1 ? "" : "s") + ")");

        } else {
            getWriter().println();
            getWriter().println("FAILURES!!!");
            getWriter().println("Tests run: " + result.getRunCount() + ",  Failures: " + result.getFailureCount());
        }
        getWriter().println();
    }

    /**
     * Returns the formatted string of the elapsed time. Duplicated from
     * BaseTestRunner. Fix it.
     */
    protected String elapsedTimeAsString(long runTime) {
        return NumberFormat.getInstance().format((double) runTime / 1000);
    }
}

更新 - Ant构建文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="COTPlus" default="main" basedir=".">
<property name="src.dir" location="src" />

<target name="test" >
<junit printsummary="on" haltonfailure="false">
  <formatter type="xml" />
  <batchtest todir="/test-reports">
    <fileset dir="${src.dir}" includes="**/ExampleTest.java"  />
  </batchtest>
</junit>
</target>

<target name="main" depends="test">
        <description>Main target</description>
        <echo>${src.dir}</echo>
    </target>

</project>

您可以使用Ant脚本来获取XML结果

<target name="test" >

 <javac srcdir="/src"
     destdir="/bin"
     classpath="/lib/junit.jar"  />


<junit haltonfailure="false">
  <formatter type="xml" />
  <batchtest todir="/test-reports">
    <fileset dir="/bin" includes="tests/ExampleTest.class"  />
  </batchtest>
</junit>
</target>

这将生成一个xml到/ test-reports文件夹中。 更多关于Ant构建的细节http://ant.apache.org/manual/tasksoverview.html

链接地址: http://www.djcxy.com/p/57985.html

上一篇: What is the best way to automatically generate JUnit results in xml?

下一篇: JNA Memory Leak