How can I repeat steps in a Specflow Scenario Outline

六眼飞鱼酱① 提交于 2020-01-23 06:13:35

问题


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


回答1:


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     |



回答2:


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...
    }
}



回答3:


This works for me and looks bit more readable too:

Scenario: common scenarios goes here one time followed by scenario outline.
   Given I am a user

Scenario Outline: To repeat a set of actions in the same page without logining in everytime.
   When I enter <x> as an amount
   Then the result should be <result>

Examples:
|x|result|
|1|2|
|2|4|
|4|8|


来源:https://stackoverflow.com/questions/7614562/how-can-i-repeat-steps-in-a-specflow-scenario-outline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!