使用具有 2 个相同场景但不同标签的功能。如何以 DRY 方式进行?

问题描述 投票:0回答:1

遇到这种情况,想知道是否有 DRYer 解决方案。

多场景的功能。 每个场景都有相同的步骤,但有不同的标签。

在场景设置之前使用标签来更改配置设置。

示例:

Feature: ABC

Tests for ABC

@tag1 @tag2 @tag3 @tag4
Scenario: Scenario for ABC
    Given step1
    When step2
    And step3
    And step4
    Then this condition matches

@tag1 @tag2 @tag3 @tag4 @tag5
Scenario: Scenario for ABC with diff config
    Given step1
    When step2
    And step3
    And step4
    Then this condition matches

对我来说感觉重复太多了。 我可以使用背景,也许有 2 个空场景,但是,使用不同的标签,但这也感觉有点脏。

关于 DRYer 解决方案有什么建议吗?

注意,在现实世界的示例中,@tag5 实际上会导致绑定中的 BeforeScenario 指向不同的数据库连接。

c# specflow
1个回答
0
投票

如果步骤完全相同,请考虑一个场景大纲,其中参数之一是步骤定义用于配置数据库连接的一些具有业务意义的名称:

Scenario Outline: Scenario for ABC
    Given the application is configured for "<customer>"
    And step1
    When step2
    And step3
    And step4
    Then this condition matches

Examples:
    | customer            |
    | City Auto Mechanics |
    | Flourists, Inc      |

步骤定义类似于:

[Given(@"the application is configured ""(.+)""")]
public void GivenTheApplicationIsConfiguredFor(string customerName)
{
    switch (customerName)
    {
        case "City Auto Mechanics":
            // configure to connect to the database for City Auto Mechanics
            break;
        case "Flourists, Inc":
            // configure to connect to the database for Flourists, Inc
            break;
        default:
            throw new NotImplementedException($"Customer {{customerName}} has no database configuration");
    }
}

基本上,您将配置数据库作为设置步骤之一,即“给定”。如果您想让 BDD 学究们满意,只需使用商业语言来表述该步骤,这样听起来就不会太技术性。

我不介意说最后一部分,因为我是那些学究之一……但那是我自己要背负的十字架,不是你的。

© www.soinside.com 2019 - 2024. All rights reserved.