在Specflow中重用两个要素文件之间的背景定义

时间:2013-11-08 12:53:41

标签: c# testing visual-studio-2012 specflow

我有几个specflow .feature文件来测试web api项目的不同实体。

对于每个实体,我必须在我的数据库中创建一个包含所有所需数据的背景,但在此之前我需要删除所有数据,并且需要使用web api调用删除(而不是通过sql查询)。我这样做:

Background: 
Given I make a new request to localhost on port 53364 
    And the path is api/voipport/deleteAll 
    And the request type is Delete
    When the request has completed
Given I make a new request to localhost on port 53364 
    And the path is api/loopblockingexception/deleteAll 
    And the request type is Delete
    When the request has completed
Given I make a new request to localhost on port 53364 
    And the path is api/stimulationcode/deleteAll 
    And the request type is Delete
    When the request has completed
Given I make a new request to localhost on port 53364 
    And the path is api/switch/deleteAllMtp3
    And the request type is Delete
    When the request has completed
Given I make a new request to localhost on port 53364 
    And the path is api/enviroment/deleteAll 
    And the request type is Delete
    When the request has completed

Given it has the following topologies
    | Id | Name  | Description  |
    | 1  | Test  | Description  |
    | 2  | Test2 | Descritpion2 |
Given it has the following eas
    | Id | TopologyId | Name  | Description  | SipListenIPAddress | SipListenPort | SipTelephonyCapacity |
    | 1  | 1          | Test  | Description1 | 127.0.0.1          | 8080          | 5                    |
Given it has the following networks
    | Id | TopologyId | NetworkTypeId | Name  | CliPrivacyRespected |
    | 1  | 1          | 2             | Test1 | Description1        |
    | 2  | 1          | 1             | Test2 | Description2        |
Given it has the following switchs
    | Id | NetworkId | SwitchType | Name          | Description              |
    | 1  | 1         | 2          | Ss7SwitchTest | Ss7SwitchTestDescription |
    | 2  | 1         | 1          | SipSwitchTest | SipSwitchTestDescription |
Given it has the following mtp3Switchs
    | Id | Name            | Description                | SignallingHost | SignallingLinkCode | SwitchId | EasId |
    | 1  | SwitchMtp3Test  | SwitchMtp3DescriptionTest  | 192.168.1.1    | 13245              | 1        | 1     |
    | 2  | SwitchMtp3Test2 | SwitchMtp3DescriptionTest2 | 192.168.1.2    | 13246              | 1        | 1     |

它工作得很完美,但我有几个.feature文件需要执行相同的deleteAll方法。我可以用其他.feature文件(或其他类型的文件)中的所有Given / When指令编写任何类型的方法,并从每个后台代码调用此方法吗?

1 个答案:

答案 0 :(得分:1)

不要忘记你的[Binding]是全球性的。从理论上讲,您可以拥有一个包含所有Given \ When \ Then的绑定文件。

另外,为什么不向WebService发送一个重置命令,而不是以这种方式构建它们?您可以启用一个新命令,例如api/testing/deleteAll,如果您使用特殊配置值运行webService,则只能使用该命令。