Gherkin/Cucumber - 我们可以将 "When" 语句分组为 "Backgound" 中的 "Given"s 吗?

Gherkin/Cucumber - Can we group "When" statements like "Given"s in "Backgound"?

我正在 Gherkin/Cucumber 迈出第一步。

我一直在通过官方文档阅读语法和 when/how 以正确使用每个标记,但是我想知道是否有适当的方法来对在内部反复使用的某些语句进行分组"When" 阶段类似于在 "Background" 部分中对 "Given" 语句进行分组?

示例:

Feature: Wanting to reduce code duplications
Background:
    Given I have done some set up
    And I have done some more set up

@Scenario_1
Scenario: An example scenario
    # Some Givens defined in Background
    Given a unique step is taken
    # When
    When I perform some action that will be repeated
        And I perform another action that will be repeated
        And I perform a non-repeated task
    # Then
    Then something will be expected at the end

@Scenario_2
Scenario: An second example scenario
    # Some Givens defined in Background
    Given a different unique step is taken
    # When
    When I perform some action that will be repeated
        And I perform another action that will be repeated
        And I perform a different non-repeated task
    # Then
    Then something different will be expected at the end

有什么方法可以对 "When" 语句进行抽象或分组:

When I perform some action that will be repeated
And I perform another action that will be repeated

进入他们自己的部分,如 "Background"?

首先,请记住 Gherkin 应该是 BDD,意思是行为驱动开发。您陈述步骤的方式不是行为而是行动。 When I perform some action that will be repeated 不描述系统或用户的任何行为。

对于重复任务,生成描述行为的 When,然后在 Java 代码中,定义您需要的所有重复步骤。

示例:

Given I go to a website
When when I click on username and typ 'User1'
And when I click on password and typ 'welcome123'
And when I click on the login button
Then the dashboard is shown
And I see that there is a proper header

基本上,在这种情况下,您是在用小黄瓜描述物理测试用例。

你真正想要达到的是:

Given I navigate to a website
When I login
Then the dashboard is shown by system

您当然可以详细说明步骤,例如:

Given I navigate to website 'http://www.google.com/' in browser 'Chrome'
When I login as user 'User1'
Then the dashboard is shown by system

这样你马上就知道测试是做什么的,用到的主要数据是什么,是否符合要求,你可以很快看到。

所以在你的情况下,尝试减少小黄瓜中的步骤描述,你的重复项将自动减少。