Cucumber: Scenario Outlines
Oct 08, 2010

Although I’ve known about scenario outlines in cucumber for sometime, I’d never used it.

There’s a lot of info on the web about this but I’ll write about it anyways. I will probably need a refresher at some point; but if it proves to be helpful for someone else, even better!

Here we go:

You can use Cucumber (Cuke) to test the expected behaviour of an application. However, there are certain cases, rather many, that the expected behaviour isn’t that simple. For example, the OR operation. If you want to test the application thoroughly, assuming two numbers, you’d have four test cases (scenarios)
Here’s where scenario outlines come to the rescue!!

Say you had simple application that takes two arguments (numbers.) When submitted, it should return the result of an OR (||) operation between them.

For a quick refresher, here’s the OR operation

0 0 = 0
0 1 = 1
1 0 = 1
1 1 = 1

The cuke Scenario would look something like this


Scenario: Entering 0 and 0
Given two numbers, 0 and 0
When entered in the form
Then I should get 0 as result

Scenario: Entering 0 and 1
Given two numbers, 0 and 1
When entered in the form
Then I should get 1 as result

And so on… (two more of these)

You’ll end up with four of these to cover all the cases and you can congratulate yourself. Your application is tested!

But as I said previously, scenario outlines can make the job cleaner and easier. And who wouldn’t want that?

Here’s what it would look like with scenario outlines

Scenario Outline: Entering two numbers to the form
Given two numbers, and
When entered in the form
Then I should get as result

Examples:
  | number1 | number2 | result |
  |      0        |      0        |   0     |
  |      0        |      1        |   1     |
  |      1        |      0        |   1     |
  |      1        |      1        |   1     |

That’s all! That’ll take care of your four cases.
As you can see “Scenario:” changed to “Scenario Outline:”
The written text was made more generic
There are variables in between brackets <>
And there’s an “Examples:” line with examples under
The variable names are at the top of the table (number1, number2, result)

Cuke will grab each of the fields in the rows, insert them in the appropriate variable and perform each example, four in total.

Four for the price of one anybody?


Comments
Name:
Comment: