我需要在 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/