使用 <junit> 立即进行 JUnit 测试日志记录蚂蚁任务 [英] Immediate JUnit test logging with <junit> Ant Task

查看:32
本文介绍了使用 <junit> 立即进行 JUnit 测试日志记录蚂蚁任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 build.xml 中有以下内容:

I have the following in my build.xml:

<junit fork="yes" printsummary="yes" filtertrace="yes">
    <classpath>...</classpath>
    <test name="tests.AllTests"/>
    <formatter type="plain" usefile="false"/>
</junit>

我希望在每个测试完成后立即报告 JUnit 结果,不幸的是,JUnit 任务仅在整个测试用例完成后才打印测试结果.测试用例(AllTests)相当大,所以我必须等待一段时间才能输出.有什么办法可以让 立即打印单个测试结果?

I would like the JUnit results to be reported for each test as soon as they complete, unfortunately the JUnit task only prints the test results after the whole test case has completed. The test case (AllTests) is rather large, so I have to wait quite a while for the output. Is there any way to make <junit> print individual test results immediately?

推荐答案

我按照 dkatzel 的建议编写了自己的 JUnitResultFormatter.这是我的 JUnitFormatter 的代码:

I followed dkatzel's suggestion to write my own JUnitResultFormatter. Here is the code for my JUnitFormatter:

package util;

import java.io.OutputStream;
import java.io.PrintStream;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;

public class SimpleTestFormatter implements JUnitResultFormatter {
    private PrintStream out = System.out;

    @Override
    public void addError(Test test, Throwable error) {
        logResult(test, "ERR");
        out.println(error.getMessage());
    }

    @Override
    public void addFailure(Test test, AssertionFailedError failure) {
        logResult(test, "FAIL");
        out.println(failure.getMessage());
    }

    @Override
    public void endTest(Test test) {
        logResult(test, "PASS");
    }

    @Override
    public void startTest(Test test) { }

    @Override
    public void endTestSuite(JUnitTest testSuite) throws BuildException { }

    @Override
    public void setOutput(OutputStream out) {
        this.out = new PrintStream(out);
    }

    @Override
    public void setSystemError(String err) {
        // don't echo test error output
    }

    @Override
    public void setSystemOutput(String out) {
        // don't echo test output
    }

    @Override
    public void startTestSuite(JUnitTest testSuite) throws BuildException { }

    private void logResult(Test test, String result) {
        out.println("[" + result + "] " + String.valueOf(test));
        out.flush();
    }
}

这就是我在 Ant 脚本中使用它的方式:

And this is how I use it in the Ant script:

<junit fork="yes" printsummary="yes" filtertrace="yes">
    <classpath>...</classpath>
    <test name="tests.AllTests"/>
    <formatter classname="util.SimpleTestFormatter" usefile="false"/>
</junit>

请注意,必须使用类路径上的 ant.jarant-junit.jar 编译该类.

Note that the class must be compiled with ant.jar and ant-junit.jar on the classpath.

这篇关于使用 &lt;junit&gt; 立即进行 JUnit 测试日志记录蚂蚁任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆