Thursday, July 8, 2010

SpecFlow scenario outlines

As many people know, SpecFlow is a tool that allows to write test scenarios in a human readable form and then run them using NUnit (other testing frameworks are also possible).

So, scenarios for SpecFlow typically look like below(I use Calculator as the simplest sample):


Scenario: TC1 Add two numbers
Given I have entered 1 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 3 on the screen

Scenario: TC2 Add two numbers
Given I have entered 2 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 4 on the screen


Obviously, In many cases number of scenarios for a single feature will be more that two, and those scenarios may differ only by values that are set as parameters.
And scenario outlines allow to define a scenario outline and then set a table of all parameters that should be passed to individual scenarios. Thus, the scenarios above may be transformed into:


Scenario Outline: TC5
Given I have entered <x> into the calculator
And I have entered <y> into the calculator
When I press add
Then the result should be <result> on the screen

Scenarios: addition
| x | y | result|
| 1 | 2 | 3|
| 2 | 2 | 4|

The scenario outline provides the same as individual scenarios (it will be the same number of tests with the same output). Moreover, when SpecFlow steps (which are defined in C#) are set for individual scenarios, they may work for Scenario outlines with no modifications. For instance, to make scenario outline above working , I didn’t write any line of code, I just changed the SpecFlow scenarios in the text editor. Using similar approach most sets of repetitive tests may be transformed to Scenario outlines at any time when it's needed.

More details on Scenario outlines are available at:
scenario outlines

2 comments:

Dmitriy Chernyavsky said...

Thanks!
I wish I had more dedication to write more user oriented posts. Currently I just note information which is essential for me, to return to you when I need

Kwull said...

Thanks a lot for the post. It's really useful.

By the way, we're always glad to see you in Brest ;)