2013-11-22 187 views
0

我已经实现了我自己的测试亚军与一个重写的runChild()方法:如何从测试运行器获取JUnit测试结果?

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 
    // ... 
    } 

    // ... 

} 

我重写此方法,因为我需要之前做一些全球前和后操作/测试用例执行之后。后续行动将取决于测试用例执行的失败/成功。我如何检索执行结果?

回答

0

我发现注册一个监听器的解决方案执行runChild()之前:

 // ... 

     // 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); 

     // ... 

但有一个更好的方式来做到这一点?

+0

你知道[Rules](https://github.com/junit-team/junit/wiki/Rules)吗? –

+0

规则必须在测试类/方法级别上实现,但我不希望我的测试知道任何有关后置动作 – user1613270