"Given, When, Then"后面可以加一个"Else"吗?

Is it possible to add an "Else" after "Given, When, Then"?

我是 Gherking 的新手,并尝试尽我所能编写我的第一个场景,但我经常发现自己真的很想在我的场景中添加 "Else"。 "Given, When, Then" 变为 "Given, When, Then, Else"。我知道 "Else" 关键字未定义,因此未在 Gherkin 工具中实现,但我不在乎,因为我不使用这些工具。

你觉得这样写对吗:

示例:

Scenario : Application starts
  Given I start the application
  When I already have an open session
  Then I see the home screen
  Else I see the login screen

还是写两个不同的场景比较好:

Scenario : Application started by authenticated user
  Given I have an open session
  When I start the application
  Then I see the home screen

Scenario : Application started by unauthenticated user
  Given I don't have an open session
  When I start the application
  Then I see the login screen

简而言之,不是,但这里有处理场景多个变体的选项:

  • 如果只是场景步骤的尾部元素不同,您可以将早期步骤移动到一个公共 'Background' 部分,使重复的​​不同场景更短更清晰。

但是从您的示例来看,所有步骤都略有不同,因此您可以:-

  • 接受多个场景的重演

  • 参数化差异并使用 'given' 和 'then' 部分中的数据 table 给出前后值。

或者(我的偏好)

  • 使用使用示例 table 的 'scenario outline' 语法来提供数据装置集及其预期结果。这些在场景步骤中替换为运行时。然后,示例中的每一行 'played out' 一次 table.

所以:

    Scenario : Application started by authenticated user
    Given I have an open session
    When I start the application
    Then I see the home screen

    Scenario : Application started by unauthenticated user
    Given I don't have an open session
    When I start the application
    Then I see the login screen

变为:

    Scenario Outline: Application Start and login
    Given Application started by <AuthenticationStatus> user
    And I have <SessionState> session
    When I start the application
    Then I see the <FirstScreen> screen

    Examples:
    |AuthenticationStatus   |SessionState   |FirstScreen|
    |Authenticated          |open           |home       |
    |Un-Authenticated       |not open       |login      |

恕我直言,在两种情况下,失去可读性可能不值得,但除此之外,我认为绝对值得。