嗨,我正在尝试使用 JUNIT 5 作为框架来实现集成测试,我们只想在执行所有测试之前启动所有过程,并在执行所有测试时停止所有过程。
我发现没有简单的方法可以做到这一点,因为我们有很多测试类,并且 BeforeAll 和 AfterAll 方法适用于每个测试类
我发现,如果我可以注册自己的自定义侦听器,我也许可以做到,但不知何故它不起作用
import com.google.auto.service.AutoService;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestPlan;
@AutoService(TestExecutionListener.class)
public class StateExecutionListener implements TestExecutionListener {
public void testPlanExecutionStarted(TestPlan testPlan) {
System.out.println("##########testPlanExecutionStarted "+testPlan.getRoots());
}
public void testPlanExecutionFinished(TestPlan testPlan) {
System.out.println("##########testPlanExecutionStarted "+testPlan.getRoots());
}
}
Run Code Online (Sandbox Code Playgroud)
知道我们如何注册我们自己的监听器/任何其他方式来检测是否所有测试用例都被执行?我使用 gradle JunitPlatform 来运行我的测试
无论如何,我们是否可以使用 store 在 JUNIT 5 中的不同扩展之间共享数据
例子
public class Extension1{
beforeAllCallback(){
getStore(GLOBAL).put(projectId,"112");
}
}
public class Extension2{
beforeTestExecutionCallback(){
System.out.println("projectId="+getStore(GLOBAL).get(projectId));
}
}
Run Code Online (Sandbox Code Playgroud)