c# - 在 Specflow 中的两个特征文件之间重用背景定义

标签 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 指令编写任何类型的方法并从每个后台代码调用此方法吗?

最佳答案

不要忘记您的 [Binding] 是全局的。理论上,您可以拥有一个包含所有 Given\When\Then 的绑定(bind)文件。

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

关于c# - 在 Specflow 中的两个特征文件之间重用背景定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19859606/

相关文章:

c# - 拆分字符串并验证每个部分

javascript - karma 测试。看不到里面的 IT block

visual-studio-2012 - TFS从分支创建分支错误 "the item does not exist at the specified version or you do not have permission to access it"

c# - 在 C# 中检查一个 VB.NET 对象是否为 null 会产生意外的编译错误

c# - 将 MySQL 中的数据库更新为 LightSwitch 中的 SQL 服务器

c# - Linq SelectMany 包含父级

ios - 建议在不使用 MacOS 或 Xcode 的情况下在 iOS(最好是 9 及更高版本)上启用开发人员选项的任何解决方法?

java - 将字符串与枚举名称匹配

asp.net-mvc - 加载符号时,Visual Studio 调试速度很慢

c# - 如何使用 Linq 或 Lambda 执行更新?(C#, Asp.net,Linq , Lambda)