0

オーバーライドされた runChild() メソッドを使用して、独自のテスト ランナーを実装しました。

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

  // ...

}

テスト ケースの実行前後にグローバルな事前アクションと事後アクションを実行する必要があるため、このメソッドをオーバーライドします。事後アクションは、テスト ケース実行の失敗/成功に依存します。実行結果を取得するにはどうすればよいですか?

4

1 に答える 1

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

        // ...

しかし、それを行うより良い方法はありますか?

于 2013-11-22T15:25:15.853 に答える