【问题标题】:Is it possible to get test status in JUnit from RunListener.testFinished?是否可以从 RunListener.testFinished 获取 JUnit 中的测试状态?
【发布时间】:2016-08-23 09:44:02
【问题描述】:

我需要在 JUnit 中以不同的方式处理失败和通过的测试。但是 JUnit RunListener 只有 testFinished() 在测试失败与否时调用,testFailed() 仅在测试失败时调用。

有没有办法从RunListener.testFinished() 找出测试结果(失败\通过)?

另外,我正在研究另一个 JUnit 类——TestWatcher。它有succeeded() 方法。但是我不想使用它,因为我还需要在所有测试完成后执行一些操作(RunListener 中的testRunFinished()

【问题讨论】:

    标签: java junit


    【解决方案1】:

    使用 RunListener 类执行此操作的一种方法是在 testFailure() 方法中使用子描述标记失败的测试,然后在 testFinished() 方法中检查该子描述:

    class MyRunListener extends RunListener {
      private static final Description FAILED = Description.createTestDescription('failed', 'failed')
    
      @Override
      void testFailure(Failure failure) throws Exception {
        failure.description.addChild(FAILED)
      }
    
      @Override
      void testFinished(Description description) throws Exception {
        if (description.children.contains(FAILED))
          // Process failed tests here...
        else
          // Process passed tests here...
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多