Cucumber Java:如何强制生成JSON插件
概述:在某些情况下,我希望中途停止运行cucumber测试包——例如当Cucumber Java:如何强制生成JSON插件,java,cucumber,cucumber-jvm,cucumber-java,cucumber-junit,Java,Cucumber,Cucumber Jvm,Cucumber Java,Cucumber Junit,概述:在某些情况下,我希望中途停止运行cucumber测试包——例如当x测试失败次数时 我可以做得很好,但是我希望在测试停止时生成json文件(plugin={json:…})。这可行吗 到目前为止我所尝试的: 调试并查看报告/插件生成的位置。当这一行执行时: Cucumber.java: runtime.getEventBus().send..... @Override protected Statement childrenInvoker(RunNotifier notif
x
测试失败次数时
我可以做得很好,但是我希望在测试停止时生成json文件(plugin={json:…}
)。这可行吗
到目前为止我所尝试的:
Cucumber.java: runtime.getEventBus().send.....
@Override
protected Statement childrenInvoker(RunNotifier notifier) {
final Statement features = super.childrenInvoker(notifier);
return new Statement() {
@Override
public void evaluate() throws Throwable {
features.evaluate();
runtime.getEventBus().send(new TestRunFinished(runtime.getEventBus().getTime()));
runtime.printSummary();
}
};
}
我希望访问runtime
字段,但它有一个private
修饰符。我还尝试通过反射访问它,但我没有完全得到我需要的。为什么不退出
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.When;
public class StepDefinitions {
private static int failureCount = 0;
private int threshold = 20;
@When("^something$")
public void do_something() {
// something
}
@After
public void after(Scenario s) {
if (s.isFailed()) ++failureCount;
}
@Before
public void before() {
if (failureCount > threshold) {
if (driver !=null) {
driver.quit();
driver = null;
}
}
}
找到了一个相当肮脏但有效的解决方案,得到了我需要的。在这里发布我的解决方案,以防任何人需要
创建自定义cucumber runner实现以获取运行时实例
public final class Foo extends Cucumber {
static Runtime runtime;
/**
* Constructor called by JUnit.
*
* @param clazz the class with the @RunWith annotation.
* @throws IOException if there is a problem
* @throws InitializationError if there is another problem
*/
public Foo(Class clazz) throws InitializationError, IOException {
super(clazz);
}
@Override
protected Runtime createRuntime(ResourceLoader resourceLoader, ClassLoader classLoader, RuntimeOptions runtimeOptions) throws InitializationError, IOException {
runtime = super.createRuntime(resourceLoader, classLoader, runtimeOptions);
return runtime;
}
}
根据使用的插件调用生成文件的同一行:
public final class ParentHook {
@Before
public void beforeScenario(Scenario myScenario) {
}
@After
public void afterScenario() {
if (your condition to stop the test) {
//custom handle to stop the test
myHandler.pleaseStop();
Foo.runtime.getEventBus().send(new TestRunFinished(Foo.runtime.getEventBus().getTime()));
}
}
}
这将要求您通过Foo.class
运行测试,例如:
@RunWith(Foo.class)
而不是@RunWith(Cucumber.class)
这里没有太多的价值,但它符合我目前的需要。我希望Cumber提供了一种开箱即用的方法。如果有更好的方法,请把它贴在这里,这样我可以接受你的答案,一旦验证