首页 > 解决方案 > Gherkin Scenario Outline 多次使用同一张表

问题描述

我正在为 Visual Studio 编写 Specflow 中的场景大纲。目的是测试人名比较器功能,以便在两者之间选择最佳名称。

就我而言,我有属于名称和外部属性的属性,它们属于 Person 实体。比较流程分为两部分:首先我检查人员(姓名的所有者)的属性以决定,如果这没有产生结果(意味着他们的属性相同),然后我检查姓名的属性. 我已经为名称的属性比较编写了单独的测试,所以在这个测试中我只关心 Person 属性和名称之间的关系——可以是Name1 < Name2:Name1 > Name2Name1 ≡ Name2.

到目前为止,我已经为这三个案例中的每一个编写了一个场景大纲,因为我需要为每个案例运行Examples表中的每个参数一次。

代码看起来像这样:

Scenario Outline: Comparing names
    Given I have a first name <name1>
    And the first person has properties <properties1>
    And I have a second name <name2>
    And the second person has properties <properties2>
    When I choose the best name
    Then the best name should be <best name>

 Examples:
 | properties1                       | properties2       |
 | FirstName:"Carlos"                | FirstName:"Johny" |
 | LastName:"Smith"                  | FirstName:"Johny" |
 | FirstName:"John",LastName:"Smith" | LastName:"Smith"  |

现在代替名称,我写了 3 次,每次为名称之间的关系写一次,我在场景中硬编码了名称。

理想情况下,我希望有一个表,以便能够有一个与表的每一行一起运行的主要参数。

知道如何在没有三个不同的场景大纲的情况下实现它吗?

标签: specflowgherkinscenarios

解决方案


用于创建每个人的 SpecFlow 表可能是理想的解决方案。这允许您传递每个名称​​的值或空值:

Scenario Outline: Comparing names
    Given I have a first name <name1>
    And the first person has properties:
        | Field      | Value          |
        | First Name | <first name 1> |
        | Last Name  | <last name 1>  |
    And I have a second name <name2>
    And the second person has properties:
        | Field      | Value          |
        | First Name | <first name 2> |
        | Last Name  | <last name 2>  |
    When I choose the best name
    Then the best name should be <best name>

Examples:
    | name1 | first name 1 | last name 1 | first name 2 | last name 2| best name |
    | X     | Carlos       |             | Johnny       |            | X         |
    | X     |              | Smith       | Johnny       |            | X         |
    | X     | John         | Smith       |              | Smith      | X         |

这种方法的优点是您可以扩展为每个人设置的属性。


推荐阅读