Matt W
Matt W

Reputation: 12434

Structuring SpecFlow tests as features

Given that Before/After-Scenario are instance method attributes but Before/After-Feature are static method attributes, is there a way to structure scenarios in a feature file so that they can benefit from each other?

I assume that if there is no way to guarantee the execution order of the scenarios then this would be a purely academic question?

Addendum: When a feature file has multiple scenarios, is the background scenario executed once per scenario or once per feature? This would complicate the answer to the above question, I think(?)

Upvotes: 0

Views: 88

Answers (1)

kfairns
kfairns

Reputation: 3067

Backgrounds

Background is run once per scenario (every scenario within that feature), i.e:

Feature: Hello World

Background:
   Given I go to the homepage
   When I search for "Hello, World!"

Scenario: See a result
   Then I should see a result

Scenario: See multiple results
   Then I should see a maximum of 10 results

Is the same as writing:

Feature: Hello World

Scenario: See a result
   Given I go to the homepage
   When I search for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Given I go to the homepage
   When I search for "Hello, World!"
   Then I should see a maximum of 10 results

Scenarios relying on other Scenarios

Scenarios should not depend on each other at all. The order in which the tests are run should be able to change without compromising functionality of the tests.

This will mean setting up your tests on a per scenario basis. One example of this is with Backgrounds (as shown above), or getting to the same point during the "Given" steps.

If I were to do this with the example above:

Feature: Hello World

Scenario: See a result
   Given I have searched for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Given I have searched for "Hello, World!"
   Then I should see a maximum of 10 results

Instead of doing something like this (struck it out so that it's clear not to do this one):

Feature: Hello World

Scenario: See a result
   Given I have searched for "Hello, World!"
   Then I should see a result

Scenario: See multiple results
   Then I should see a maximum of 10 results

Upvotes: 1

Related Questions