我正在尝试收集有关系统中运行的测试的数据。测试来自外部 jar:
Class<?> classToLoad = Class.forName ("tests.tests", true, child);
RunListener rl = new ExecutionListener();
JUnitCore jUnit = new JUnitCore();
jUnit.addListener(rl);
Result result = jUnit.runClasses(classToLoad);
ExecutionListener 是:
public class ExecutionListener extends RunListener
{
private StringBuilder _strBuilder;
public ExecutionListener()
{
_strBuilder = new StringBuilder();
}
@Override
public String toString()
{
return _strBuilder.toString();
}
/**
* Called before any tests have been run.
* */
@Override
public void testRunStarted(Description description) throws java.lang.Exception
{
_strBuilder.append("Number of testcases to execute : " + description.testCount());
}
/**
* Called when all tests have finished
* */
@Override
public void testRunFinished(Result result) throws java.lang.Exception
{
_strBuilder.append("Number of testcases executed : " + result.getRunCount());
}
/**
* Called when an atomic test is about to be started.
* */
@Override
public void testStarted(Description description) throws java.lang.Exception
{
_strBuilder.append("Starting execution of test case : "+ description.getMethodName());
}
/**
* Called when an atomic test has finished, whether the test succeeds or fails.
* */
@Override
public void testFinished(Description description) throws java.lang.Exception
{
_strBuilder.append("Finished execution of test case : "+ description.getMethodName());
}
/**
* Called when an atomic test fails.
* */
@Override
public void testFailure(Failure failure) throws java.lang.Exception
{
_strBuilder.append("Execution of test case failed : "+ failure.getMessage());
}
/**
* Called when a test will not be run, generally because a test method is annotated with Ignore.
* */
@Override
public void testIgnored(Description description) throws java.lang.Exception
{
_strBuilder.append("Execution of test case ignored : "+ description.getMethodName());
}
}
但是没有一个函数被调用...所以(rl.toString()
返回空字符串)。
我该如何修复它?
提前致谢!
最佳答案
有同样的问题,不知道为什么,但这似乎不适用于 JUnitCore .runClasses() 但确实适用于 JUnitCore.run()。
我意识到这是一个 4 岁的 child ,但考虑到我今天遇到这个问题,我认为值得发布一个答案。
关于java - JUnit - 未调用 RunListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16901268/