Question

Is there any way to reuse data in SpecFlow feature files? E.g. I have two scenarios, which both uses the same data table:

Scenario: Some scenario 1
  Given I have a data table
     | Field Name | Value |
     | Name       | "Tom" |
     | Age        |  16   |
  When ...

Scenario: Some scenario 2
  Given I have a data table
     | Field Name | Value |
     | Name       | "Tom" |
     | Age        |  16   |
  And I have another data table
     | Field Name | Value     |
     | Brand      | "Volvo"   |
     | City       |  "London" |
  When ...

In these simple examples the tables are small and there not a big problem, however in my case, the tables have 20+ rows and will be used in at least 5 tests each.

I'd imagine something like this:

Having data table "Employee"
     | Field Name | Value |
     | Name       | "Tom" |
     | Age        |  16   |

Scenario: Some scenario 1
  Given I have a data table "Employee"
  When ...

Scenario: Some scenario 2
  Given I have a data table "Employee"
  And I have another data table
     | Field Name | Value     |
     | Brand      | "Volvo"   |
     | City       |  "London" |
  When ...

I couldn't find anything like this in SpecFlow documentation. The only suggestion for sharing data was to put it into *.cs files. However, I can't do that because the Feature Files will be used by non-technical people.

Was it helpful?

Solution

The Background is the place for common data like this until the data gets too large and your Background section ends up spanning several pages. It sounds like that might be the case for you.

You mention the tables having 20+ rows each and having several data tables like this. That would be a lot of Background for readers to wade through before the get to the Scenarios. Is there another way you could describe the data? When I had tables of data like this in the past I put the details into a fixtures class in the automation code and then described just the important aspects in the Feature file.

Assuming for the sake of an example that "Tom" is a potential car buyer and you're running some sort of car showroom then his data table might include:

| Field         | Value |
| Name          | Tom   |
| Age           | 16    |
| Address       | ....  |
| Phone Number  | ....  |
| Fav Colour    | Red   |
| Country       | UK    |

Your Scenario 2 might be "Under 18s shouldn't be able to buy a car" (in the UK at least). Given that scenario we don't care about Tom's address phone number, only his age. We could write that scenario as:

Scenario: Under 18s shouldnt be able to buy a car
    Given there is a customer "Tom" who is under 16
    When he tries to buy a car
    Then I should politely refuse

Instead of keeping that table of Tom's details in the Feature file we just reference the significant parts. When the Given step runs the automation can lookup "Tom" from our fixtures. The step references his age so that a) it's clear to the reader of the Feature file who Tom is and b) to make sure the fixture data is still valid.

A reader of that scenario will immediately understand what's important about Tom (he's 16), and they don't have to continuously reference between the Scenario and Background. Other Scenarios can also use Tom and if they are interested in other aspects of his information (e.g. Address) then they can specify the relevant information Given there is a customer "Tom" who lives at 10 Downing Street.

Which approach is best depends how much of this data you've got. If it's a small number of fields across a couple of tables then put it in the Background, but once it gets to be 10+ fields or large numbers of tables (presumably we have many potential customers) then I'd suggest moving it outside the Feature file and just describing the relevant information in each Scenario.

OTHER TIPS

Yes, you use a background, i.e. from https://github.com/cucumber/cucumber/wiki/Background

Background:
  Given I have a data table "Employee"
  | Field Name | Value |
  | Name       | "Tom" |
  | Age        |  16   |

Scenario: Some scenario 1
  When ...

Scenario: Some scenario 2
  Given I have another data table
  | Field Name | Value     |
  | Brand      | "Volvo"   |
  | City       |  "London" |

If ever you aren't sure I find http://www.specflow.org/documentation/Using-Gherkin-Language-in-SpecFlow/ a great resource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top