gpt4 book ai didi

java - 自定义 Junit 运行程序,运行之前和之后

转载 作者:行者123 更新时间:2023-12-02 12:46:25 25 4
gpt4 key购买 nike

在单元自定义运行程序中,我想在运行测试操作之前和之后执行操作,所以我想出了这个解决方案。

这样做有多可靠,有没有更干净的方法来实现这一点?

public class SomeCustomRunner extends BlockJUnit4ClassRunner {
private int m_testMethodIndex = 0;
private int m_testMethodsCount = 0;
private boolean m_sessionSetup = false;

@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
if(m_sessionSetup == false) {
m_sessionSetup = true;
beforeTestClass(); //->> DO MY STUFF HERE
}

super.runChild(method, notifier);

m_testMethodIndex++;

if(m_testMethodIndex == m_testMethodsCount) {
afterTestClass(); //->> DO MY STUFF HERE
}
}

@Override
protected List<FrameworkMethod> getChildren() {
List<FrameworkMethod> methods = super.getChildren();
m_testMethodsCount = methods.size();
return methods;
}
}

最佳答案

您可以在使用 @BeforeClass 注释的方法中定义在测试类本身之前和之后执行的操作,而不是创建单独的测试运行程序。 @AfterClass

要在多个测试中重用它们,您可以轻松地从基类继承它们。

关于java - 自定义 Junit 运行程序,运行之前和之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44759618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com