如何在多次执行相同方法时获取ScenarioContext?

时间:2018-05-31 11:02:22

标签: c# specflow

示例功能1:

Scenario: Record Creation for different Users
   Given "User 1" is created a record
    And "User 2" is created a record
    And "User 3" is created a record
    Then admin is able to check all the record IDs

示例功能2:

Scenario: Record Creation for guest User
   Given "guest User 1" is created a record
    Then admin is able to check all the record IDs

在这里,我有一个步骤2,3和5的通用规范步骤,并将从步骤2,3和4获得一些唯一ID。在步骤5中,我需要验证从步骤2生成的所有唯一ID ,3和3.

示例步骤定义:

    [Given(@"""(.*)"" is created a record")]
    public void GivenIsCreatedARecord(string username)
    {
        var recordRef=<<some Logic >>;
        ScenarioContext.Current["Record Ref"] = recordRef;
    }

我已经分析并使用ScenarioContext来共享所有步骤之间的信息。在这里,我在场景上下文中设置记录参考值,并在最后一步中访问它以进行验证(var expectedID=ScenarioContext.Current["Record Ref"])。但是,实现的逻辑对于功能2工作正常,并且用户3的ID正在针对功能1进行更新(因为,相同的方法执行3次)。

有没有办法在某些列表中保存User1 ID,User2 ID和Uuser 3 ID,并且在验证步骤中可以访问相同的内容(所有ID信息只需要在场景中访问)?

1 个答案:

答案 0 :(得分:0)

您可以使用场景大纲

Scenario Outline: Record Creation for different Users
 Given '<user>' is created a record
 Then admin is able to check all the record IDs
 Examples:
 |user  |
 |User 1|
 |User 2|
 |User 3|

测试代码

private Dictionary<string,SomeType> usersResult= new Dictionary<string,SomeType>();
[Given(@"""(.*)"" is created a record")]
public void GivenIsCreatedARecord(string username)
{
    var recordRef=<<some Logic >>;
    usersResult.add(username,recordRef);
    //other stuff
}