我有15个带有测试的JUnit5类。当我从maven运行它们时, afterAll()被执行15次,这将导致15条通向Slack Webhook的通知。我还需要发送一条通知吗?
public class TestResultsExtensionForJUnit5 implements TestWatcher, AfterAllCallback {
@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
sendResultToWebHook();
}
@Override
public void testDisabled(ExtensionContext context, Optional<String> reason) {
totalTestDisabled = totalTestDisabled + 1;
}
@Override
public void testSuccessful(ExtensionContext context) {
totalTestPassed = totalTestPassed + 1;
}
@Override
public void testAborted(ExtensionContext context, Throwable cause) {
totalTestAborted = totalTestAborted + 1;
}
@Override
public void testFailed(ExtensionContext context, Throwable cause) {
totalTestFailed = totalTestFailed + 1;
}
}
@ExtendWith(TestResultsExtensionForJUnit5.class)
public class Random1Test {}
答案 0 :(得分:3)
最佳方法是从《 JUnit平台》实现并安装TestExecutionListener
,如《用户指南》中的https://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom所述-在通知时覆盖默认的testPlanExecutionFinished(TestPlan testPlan)
方法呼叫。到此,来自 all 引擎的 all 测试已经完成。