如何从测试运行程序获取JUnit测试结果?
我使用重写的runChild()方法实现了自己的测试运行程序:如何从测试运行程序获取JUnit测试结果?,junit,junit-runner,Junit,Junit Runner,我使用重写的runChild()方法实现了自己的测试运行程序: 我重写这个方法是因为我需要在测试用例执行之前/之后执行一些全局的pre-and-post操作。post操作将取决于测试用例执行的失败/成功。如何检索执行结果?我在执行runChild()之前注册了一个侦听器,找到了一个解决方案: 但是有更好的方法吗?规则必须在测试类/方法级别上实现,但我不想让我的测试知道关于post操作的任何信息 public class MyTestRunner extends BlockJUnit4ClassR
我重写这个方法是因为我需要在测试用例执行之前/之后执行一些全局的pre-and-post操作。post操作将取决于测试用例执行的失败/成功。如何检索执行结果?我在执行runChild()之前注册了一个侦听器,找到了一个解决方案:
但是有更好的方法吗?规则必须在测试类/方法级别上实现,但我不想让我的测试知道关于post操作的任何信息
public class MyTestRunner extends BlockJUnit4ClassRunner {
// ...
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
if (method.getAnnotation(Ignore.class) != null) {
return;
}
// Do some global pre-action
// ...
// Runs the passed test case
super.runChild(method, notifier);
// Do some global post-action depending on the success of the test case
// ...
}
// ...
}
// ...
// Add callback listener to do something on test case success
notifier.addListener(new RunListener() {
@Override
public void testRunFinished(Result result) throws Exception {
super.testRunFinished(result);
if (result.getFailureCount() == 0) {
// Do something here ...
}
}
});
// Runs the passed test case
super.runChild(method, notifier);
// ...