Behave - Common features between applications, avoiding duplication

571 views Asked by At

I have many applications which I want to test, which have a largely overlapping set of features. Here is an oversimplified example of a scenario I might have:

Given <name> is playing a game,
 When they shoot at a <color> target
 Then they should <event>

Examples: 
 | name   | color | event |
 | Alice  | red   | hit   |
 | Alice  | blue  | miss  |
 | Bob    | red   | miss  |
 | Bob    | blue  | hit   |
 | Bob    | green | hit   |

It's a silly example, but suppose really I have a lot of players with different hit/miss conditions, and I want to run just the scenarios for a given name? Say, I only want to run the tests for Alice. There's still advantage to having all the hit/miss tests in a single Scenario Outline (since, after all, they're all closely related).

One approach would be to just duplicate the test for every name and tag them, so something like:

@Alice
Given Alice is playing a game
 When she shoots at a <color> target
 Then she should <event>

Examples:
 | color | event |
 | red   | hit   |
 | blue  | miss  |

This way I can run behave --tags @Alice, But then I'm repeated the same scenario for every user, and that's a lot of duplication. Is there a good way to still compress all the examples into one scenario - but only selectively run some of them? What's the right approach here?

1

There are 1 answers

0
Louis On BEST ANSWER

Version 1.2.5 introduced better ways to distinguish scenario outlines. It is now possible to uniquely distinguish them and thus select a unique scenario generated from an outline with --name= at the command line. For instance, suppose the following feature file:

Feature: test

Scenario Outline: test
 Given <name> is playing a game,
 When they shoot at a <color> target
 Then they should <event>

 Examples:
  | name   | color | event |
  | Alice  | red   | hit   |
  | Alice  | blue  | miss  |
  | Bob    | red   | miss  |
  | Bob    | blue  | hit   |
  | Bob    | green | hit   |

Let's say I want to run only the test for Bob, red, miss. It is in the first table, 3rd row. So:

behave --name="@1.3"

will select this test. In version 1.2.5 and subsequent versions. A generated scenario gets a name which includes "@<table number>.<row number>" where <table number> is the number of the table (starting from 1) and <row number> is the number of the row.

This won't easily allow you to select all scenarios that pertain to a single user. However, you can achieve it in another way. You can split your examples in two:

 Examples: Alice
  | name   | color | event |
  | Alice  | red   | hit   |
  | Alice  | blue  | miss  |

 Examples: Bob
  | name   | color | event |
  | Bob    | red   | miss  |
  | Bob    | blue  | hit   |
  | Bob    | green | hit   |

The table names will appear in the generated scenario names and you could ask behave to run all the tests associated with one table:

behave --name="Alice"

I do not know of a way to access the example name in steps and thus get rid of the first column.

The full set of details is in the release notes for 1.2.5.