我可以从任何 JUnit 5 侦听器更改测试的状态(通过、失败、跳过)吗?

问题描述 投票:0回答:1

在 TestNG 中,我可以使用

ITestListener
设置测试的最终状态。

示例:

public class TestListener implements ITestListener {
    
    @Override
    public void onTestSuccess(ITestResult result) {
       result.setStatus(ITestResult.FAILURE);    
    }

}

这可以在 JUnit 5 中完成吗?如何实现?

java junit5
1个回答
0
投票

您可以从

org.junit.jupiter:junit-jupiter-api"
使用 TestWatcher 界面:

依赖性:

testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")

使用:

public class SomeTest implements TestWatcher, AfterAllCallback {
    public void testDisabled(ExtensionContext context, Optional<String> reason) {
        
    }
}
  • 接口的所有方法都是默认“实现”的,因此您不必重写所有方法。
  • AfterAllCallback 接口允许您处理
    afterAll()
    方法
© www.soinside.com 2019 - 2024. All rights reserved.