Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I repeat steps in a Specflow Scenario Outline

Tags:

specflow

In a nutshell, what I need is to create a Scenario Outline with a step that is repeatable without having to type it in using multiple AND's as I am currently doing below:

Scenario Outline: outline
    Given I am a user
    When I enter <x> as an amount
       And I enter <x2> as an amount
    Then the result should be <result>
Scenarios:
    |x|x2|result|
    |1|2 |3     |
    |1|0 |1     |

However, I would like to do something like the following:

Scenario Outline: outline 
    Given I am a user
    When I enter <Repeat: x> as an amount
    Then the result should be <result>

Scenarios:
    |x    |result|
    |1,2,3|6     |
    |1,2  |3     |

Basically, I want the "I enter as an amount" to run 3 and 2 times respectively.

The closest I have found to this question is How do I re-run a cucumber scenario outline with different parameters? , but I wanted to double check before giving up and using a StepArgumentTransformation with a comma separated list or something similar.

The final answer that I went with is something more like this:

Scenario Outline: outline 
    Given I am a user
    When I enter the following amounts
        | Amount1 | Amount 2| Amount3|
        | <Amt1>  | <Amt2>  | <Amt3> | 
    Then the result should be <result>

Scenarios:
    |Amt1 |Amt2 |Amt3 |result|
    |1    |2    |3    |6     |

There does not seem to be a good way to leave a value blank though, but this is pretty close to the solution I was looking for

like image 402
Justin Pihony Avatar asked Sep 30 '11 18:09

Justin Pihony


People also ask

How do you run the same scenario multiple times in SpecFlow?

You can set up multiple test targets in your SpecFlow runner profile, and it will run the tests once for each defined target. This allows us to automatically run the same test suite multiple times, with different set-up values.

Can we have multiple examples for same scenario outline?

Each scenario outline can have multiple tables with examples. The parameters in scenario outlines allow test runners to paste the examples from the table into the outline.

What is the difference between scenario and scenario outline in SpecFlow?

Example keyword can only be used with the Scenario Outline Keyword. Scenario Outline - This is used to run the same scenario for 2 or more different sets of test data. E.g. In our scenario, if you want to register another user you can data drive the same scenario twice.

What is the difference between background and scenario outline?

Use scenario outlines to group examples that share the same structure, and put common data in the scenario instead of the example tables. Use background sections to expose contextual information for examples that do not share the same structure.


2 Answers

The Examples keyword:

Scenario Outline: outline
    Given I am a user
    When I enter <x> as an amount
    Then the result should be <result>
    Examples:
        |x|result|
        |1|3     |
        |1|1     |

Is for when you want the entire test to take different parameters. It sounds like you want to feed repeating parameters at the When step.

The easier approach that won't require implementation of a StepArgumentTransformation is to simply use a table in the when:

When I enter the following amounts
|Amount|
|2     |
|3     |
|4     |

Then simply iterate all the rows in table to get your arguments. This avoids needing to have an interim method for the transformation.

Alternatives include using more steps or by parameter parsing, so as you say, use the StepArgumentTransformation.

Of course, if you need to test multiple repeating items, you could use both StepArgumentTransformation and Examples: feeding in a comma-list of numbers:

Examples:
|x    |result|
|1    |1     |
|1,2,3|6     |
like image 89
Adam Houldsworth Avatar answered Sep 21 '22 13:09

Adam Houldsworth


You might be able to make use make use of an approach patrickmcgraw suggested on an answer to my question:

SpecFlow/Cucumber/Gherkin - Using tables in a scenario outline

So basically you could take the input x as a normal input string split it on a delimiter in this case ',' and then iterate over it performing your actions i.e. something like below (I haven't tested this code).

[When(@"I enter (.*) as an amount")]
public void IEnterAsAnAmount(string x)
{
   var amounts = x.Split(',');
    foreach (var amount in amounts)
    {
        // etc...
    }
}
like image 32
MattStacey Avatar answered Sep 18 '22 13:09

MattStacey