如何在SpecFlow场景背景中使用动态参数?

时间:2014-11-18 13:42:14

标签: specflow

我有一项功能可以登录交易系统并锁定多个交易。每笔交易开始时都有很多可重复使用的步骤(初始交易设置)但是每笔交易都有不同的论点。

这是一个例子

Scenario: Trade 1

Given I have selected my test data: "20003"
And I have connected to VMS with the following details:
| Field    | Value    |
| Username | user     |
| Password | password |
| Session  | myServer |

When I run the DCL command to set my privileges to full
Then I expect to see the following:
| Pass Criteria  | Timeout  |
| Privileges Set | 00:00:30 |

When I ICE to the test account: "System Test"
Then I expect to be ICED see the following:
| Pass Criteria         | Timeout    |
| "ICED to System Test" | "00:00:10" |

When I run a dcl to delete the company: "Test_Company"
Then I expect to see a confirmation that company: "Test_Company" has been deleted or doesnt exist

因此,在这些步骤中,可以改变的两件事是"给定"参数所以测试数据ID和最后的测试公司。

我想要的是运行后台步骤的一些方法,以便能够知道要输入的参数。因此,如果它是Trade 1,例如它将进入20003,如果它是Trade 2进入20004等。 我可以这样做吗?我正在考虑使用"示例"场景大纲使用的表。或者有更好的方法吗?我不希望在我的所有场景中都有这些可重复的步骤,因为它占用了大量空间并且看起来不太可读。

1 个答案:

答案 0 :(得分:1)

所以我做了一些搜索,无法找到一个不需要大量编码的解决方案,所以我做了这个:

这就是背景的样子

Background:
Given I have selected my test data:
| Scenario          | ID    |
| DirectCredit_GBP  | 20003 |
| Cheque_GBP        | 20004 |
| ForeignCheque_GBP | 20005 |

为了找到它应该使用哪一行,它后面的方法使用ScenarioContext。这是方法:

    [Given(@"I have selected my test data:")]
    [When(@"I have selected my test data:")]
    public static void setTestDataID(Table data)
    {
        string scenario = ScenarioContext.Current.ScenarioInfo.Title;
        string testDataId = data.ReadTable("Scenario", scenario, "ID"));
        TestDriver.LoadTestData(testDataId);
    }

该方法的作用是在表格中搜索方案名称(使用我编写的扩展方法)并获取ID,一旦获得ID,它就会将其传递给我的TestDriver方法。

似乎工作正常并使测试可读。

相关问题