I have a little over a month working with SpecFlow and got to a point where I configured a Background Scenario to setup/verify common data on a database, so the next step was trying to reuse the background for several feature files, to avoid cutting and pasting.
It has been asked before but I expected something else, more user-friendly, just as the Background scenario is easy to understand and update:
Background:
Given I have created the following currencies:
| Code | Name |
| USD | United States Dollar |
| EUR | Euro |
And I have created the following countries:
| Code | Currency | Name |
| US | USD | United States |
| ES | EUR | Spain |
| IT | EUR | Italy |
I found a quite naive solution that is working (or at least seems to, so far), but I'm concerned it may lead me the wrong way, because of my shallow knowledge of SpecFlow.
Taking a look at the generated code for a feature file I got to this:
Create a "feature" file that only has the background scenario, named something like "CommonDataSetup"
Create a step definition like:
[Given(@"common data configuration has been verified")] public void GiveCommonDataConfigurationHasBeenVerified() { // this class is inside the generated feature file var commonSetup = new CommonDataSetupFeature(); var scenarioInfo = new ScenarioInfo("Common data configuration", ((string[])(null))); commonSetup.FeatureSetup(); commonSetup.ScenarioSetup(scenarioInfo); commonSetup.FeatureBackground(); commonSetup.ScenarioCleanup(); commonSetup.FeatureTearDown(); }In the Background of the other feature files write:
Background: Given common data configuration has been verified
So now I can reuse the "common data configuration" step definition in as many feature files I need keeping DRY, and background scenarios can be much shorter.
I seems to work fine, But I wonder, is this a the right way to achieve background reuse?
Thanks in advance.