问题
I have several specflow .feature files to test different entities of a web api project.
For every entity I have to create a background with all the needed data in my database, but before of all I need to delete all data, and need to delete using web api calls (not by sql query). I'm doing this:
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 |
It works perfect, but I have several .feature files that need to execute the same deleteAll methods. Can I write any kind of method with all the Given/When instructions in other .feature file (or another kind of file) and call this method from every background code?
回答1:
Dont forget that your [Binding]
s are global. In theory you could have one binding file with all your Given
\ When
\ Then
s in it.
Also, why don't you send your WebService a single reset command, rather than building them up in this manner? You could enable a new command such as api/testing/deleteAll
which is only availble if you run your webService with a special config value.
来源:https://stackoverflow.com/questions/19859606/reuse-background-definition-between-two-feature-files-in-specflow