使用变量重复相同的场景

Repeat the same scenario with a variable

我正在使用 Behat 测试一个网络应用程序 API 的用户权限。我需要确保多个角色无法访问某些 API。因此,我需要检查这些角色是否收到来自 API 的 Forbidden 响应。这很好用,但是由于有 8 个不同的角色,我的功能文件变得越来越大,因为我为每个角色重复了所有步骤..

我现在的特征是这样写的:

Feature: Accounting

  @accounting
  Scenario: I want to see the accounting
    Given I have the role of "sales"
    When I want to get the accounting
    Then I should get a forbidden response
    When I want to get the balance sheet
    Then I should get a forbidden response
    When I want to get the income statement
    Then I should get a forbidden response

    Given I have the role of "project"
    When I want to get the accounting
    Then I should get a forbidden response
    When I want to get the balance sheet
    Then I should get a forbidden response
    When I want to get the income statement
    Then I should get a forbidden response

    Given I have the role of "support"
    When I want to get the accounting
    Then I should get a forbidden response
    When I want to get the balance sheet
    Then I should get a forbidden response
    When I want to get the income statement
    Then I should get a forbidden response

    ...

只有角色名称变了,其他都一样。我想知道是否有办法多次重新执行一个场景但输入不同?或者可能有更好的方法来处理这种情况?

这就是 scenario outlines 的用途:

  Scenario Outline: I want to see the accounting
    Given I have the role of "<role>"
    When I want to get the accounting
    Then I should get a forbidden response
    When I want to get the balance sheet
    Then I should get a forbidden response
    When I want to get the income statement
    Then I should get a forbidden response

    Examples:
      | role    |
      | sales   |
      | project |
      | support |