【问题标题】:How to write a custom Junit runner that will log results如何编写将记录结果的自定义 Junit 运行器
【发布时间】:2013-10-23 21:10:53
【问题描述】:

我正在使用 JUnit,我有这个想法,我想实现它。 我想编写一个运行器,它将每个测试的结果记录到一个 excel 或文本文件中,以便我可以将它附加到我的报告中。 我需要学习什么才能开始

【问题讨论】:

    标签: junit4


    【解决方案1】:

    两种选择

    1. 编写一个 RunListener 并像这样使用它:

      public void main(String... args) { 
          JUnitCore core= new JUnitCore();
          core.addListener(new MyRunListener());
          core.run(MyTestClass.class);
      }
      
    2. 再写一个RunListener。但是这次扩展了一个 org.junit.runner.Runner 实现并覆盖了它的 run 方法,比如

      @Override
      public void run(RunNotifier notifier) {
          notifier.addListener(new MyRunNotifier());
          super.run(notifier);
      }
      

    第二种方法也可以用在带有 @RunWith(MyRunner.class) 注释的测试中。

    【讨论】:

    • 在解决方案(2)中,super.run 不起作用,因为 Runner 是抽象的。你是说“ParentRunner”吗?
    • 正如我所提到的,“扩展 Runner 实现”。所以 super.run 会调用扩展的 ParentRunner。
    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2015-04-09
    • 2011-03-08
    • 2014-01-26
    • 2021-09-02
    相关资源
    最近更新 更多