【问题标题】:How to get a reference of cucumber RuntimeOptions when using cucumber-java and junit?使用 cucumber-java 和 junit 时如何获取 cucumber RuntimeOptions 的引用?
【发布时间】:2014-03-06 12:44:28
【问题描述】:

我在 groovy 代码中使用 cucumber-java。我更喜欢 cucumber-java 而不是 cucumber-groovy,因为我可以像普通的老式 JUnit 测试一样运行测试。但是cucumber吐出来的步骤定义模板是java风格的。相反,我想要一种时髦的风格。例如,在 java 风格中,你会得到类似的东西

@When("^an HTTP GET request is sent to obtain config.xml of \"([^\"]*)\"$")
public void an_HTTP_GET_request_is_sent_to_obtain_config_xml_of(String arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

由于我使用的是 groovy,所以我想获得类似的东西

@When(/^an HTTP GET request is sent to obtain config.xml of "([^"]*)"$/)
void 'an HTTP GET request is sent to obtain config.xml of'(String arg1) {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

我正在考虑实现这样的功能。基本上,我的想法是在cucumber.api.CucumberOptions 中添加一个新字段,可能称为templateLanguage。当这个新字段等于groovy 时,groovy 样式的模板将被吐出。这可能会涉及到cucumber.runtime.java.JavaSnippet.template() 中的if 语句,例如
if( runtimeOptions.getTemplateLanguage().toLowerCase().equals('groovy') ) {...}
但是,我的问题是:如何获取传入的 runtimeOptions 的引用,如

@CucumberOptions(
        format = ["pretty", "html:build/cucumber"],
        features="src/test/resources/cucumber_features/api/job_view.feature",
        glue=['com.yahoo.adcd.jenkins.tests.smoke.api.cucumber.job.view'],
        strict = true
)

非常感谢!

【问题讨论】:

标签: java groovy junit cucumber cucumber-jvm


【解决方案1】:

在这种情况下,您需要编写自己的引导类,因为RuntimeOptions 没有依赖注入。一个好的起始位置是查看cucumber.api.cli.Main。您需要创建自己的扩展 RuntimeOptions 的类,然后在此处添加您的逻辑。

但是,此解决方案将不再允许您使用 CucumberOptions 注释运行应用程序。如果您确实更喜欢使用注解,您还需要实现自己的自定义注解并覆盖 RuntimeOptionsFactory 以使用您的注解,然后在新的主类中使用该工厂来动态创建运行时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多