Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从测试运行程序获取JUnit测试结果?_Junit_Junit Runner - Fatal编程技术网

如何从测试运行程序获取JUnit测试结果?

如何从测试运行程序获取JUnit测试结果?,junit,junit-runner,Junit,Junit Runner,我使用重写的runChild()方法实现了自己的测试运行程序: 我重写这个方法是因为我需要在测试用例执行之前/之后执行一些全局的pre-and-post操作。post操作将取决于测试用例执行的失败/成功。如何检索执行结果?我在执行runChild()之前注册了一个侦听器,找到了一个解决方案: 但是有更好的方法吗?规则必须在测试类/方法级别上实现,但我不想让我的测试知道关于post操作的任何信息 public class MyTestRunner extends BlockJUnit4ClassR

我使用重写的runChild()方法实现了自己的测试运行程序:


我重写这个方法是因为我需要在测试用例执行之前/之后执行一些全局的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);

        // ...