【问题标题】:Cucumber scenario outline and examples with generic step definitionsCucumber 场景大纲和带有通用步骤定义的示例
【发布时间】:2017-06-28 05:27:42
【问题描述】:

我有一个功能文件如下:

Scenario Outline: Create ABC

  Given I open the application

  When I enter username as <username>

  And I enter password as <password>

  Then I enter title as <title>

  And press submit


Examples:

| username | password | title |

| Rob      | xyz1      | title1 |

| Bob      | xyz1      | title2 |

这要求我对这些值中的每一个都有步骤定义。我可以换一个

可以为每个用户名或密码或标题值映射的通用步骤定义

示例部分。

也就是说,而不是说

@When("^I enter username as Rob$")
public void I_enter_username_as_Rob() throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

我可以进入吗

@When("^I enter username as <username>$")
public void I_enter_username_as_username(<something to use the value passed>) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

【问题讨论】:

  • 怎么会这样?一定是老黄瓜插件,现在的outline已经很好支持了,在的情况下只产生一个通用的step函数。无需更改为“”
  • 当通过 JUnit 执行时仍然会在 CucumberJVM 1.2.5 上发生。所以使用引号。 :-)

标签: cucumber cucumber-jvm


【解决方案1】:

你应该使用这种格式

Scenario Outline: Create ABC

    Given I open the application
    When I enter username as "<username>"
    And I enter password as "<password>"
    Then I enter title as "<title>"
    And press submit

这会产生

@When("^I enter username as \"([^\"]*)\"$")
public void I_enter_username_as(String arg1) throws Throwable {
    // Express the Regexp above with the code you wish you had
    throw new PendingException();
}

arg1 现在将传递您的用户名/值。

【讨论】:

  • 谢谢!我还想解释一下 \"([^\"]*)\" 是一个正则表达式,它会查找以引号 " 开头并以未定义数量的其他字符结尾的字符串(星号 * 提供)。因此,如果我们查看“”,我们可以看到它以引号“”开头。希望对某人有所帮助。
  • 正如@hipokito 所澄清的那样,作为一种替代方案@When("^I enter username as \"(.+)\"$") 也是如此
【解决方案2】:

Cucumber 会自动给出控制台中缺少的步骤。只需进行试运行,缺少的步骤就会显示在控制台中。

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty" }, features = { "<path_to_feature>" }, 
    glue = { "<optional_steps_location_java_file>" }, dryRun = true,
    tags = { "<optional_NOT_req_for_now>" })
public class RunMyCucumberTest {

}

See for more Cucumber options

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多