我按照 dkatzel 的建议编写了自己的JUnitResultFormatter
. 这是我的 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 脚本中使用它的方式:
<junit fork="yes" printsummary="yes" filtertrace="yes">
<classpath>...</classpath>
<test name="tests.AllTests"/>
<formatter classname="util.SimpleTestFormatter" usefile="false"/>
</junit>
请注意,必须使用类路径编译ant.jar
该类ant-junit.jar
。