java - 是否可以从 RunListener.testFinished 获取 JUnit 中的测试状态?

标签 java junit

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

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

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

最佳答案

使用 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...
  }
}

关于java - 是否可以从 RunListener.testFinished 获取 JUnit 中的测试状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39097716/

相关文章:

java - Android:按钮被禁用,仅在执行对话框功能后才起作用

java - 大输入数组中的奇数异或对

java - 如何在多线程中使用等待和通知协议(protocol)

android - Robolectric:actionBar.hide() 返回 Null

java - JUnit并发访问synchronizedSet

java - 我们如何使用mockito来获取对象

java - 如何直观地打印非二叉树?

java - JUnit 中的 PowerMock

java - 对 JUnit 测试进行分组

java - 使用模拟对象进行 junit 测试 : stub inner function calls