Immediate JUnit test logging with <junit> Ant Task

♀尐吖头ヾ 提交于 2019-12-07 05:30:00

问题


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>

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?


回答1:


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();
    }
}

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>

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




回答2:


I believe the built-in JUnit formatters buffer everything until the end. You should be able to write your own formatter implementation which extends org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter so that it flushes to STDOUT immediately, but I've never done it.

You can use this blog where the author makes a custom formatter and shows both the code and how to use it in the build.xml http://shaman-sir.wikidot.com/one-liner-output-formatter

There is also a similar SO question with similar info How do I configure JUnit Ant task to only produce output on failures?



来源:https://stackoverflow.com/questions/18293054/immediate-junit-test-logging-with-junit-ant-task

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!