I have Scenario-1, Scenario-2, and Scenario-3 that has 5 steps common.
Example:
Scenario-1:
Given User Step1
And User Step2
And User Step3
And User Step4
And User Step5
When User Completes 5 steps in X
Then User Signoff X
Scenario-2:
Given User Step1
And User Step2
And User Step3
And User Step4
And User Step5
When User Completes 5 steps in Y
Then User Signoff Y
Scenario-3:
Given User Step1
And User Step2
And User Step3
And User Step4
And User Step5
When User Completes 5 steps in Z
Then User Signoff Z
Instead of writing all the common 5 steps everytime can I call like Step1to5
Scenario-N
Given User Step1to5
When User Completes 5 steps in N
Then User Signoff N
Note: I came to know that in Specflow caling Steps from Step is removed in latest version, If calling Steps from Step is one option and I referred to JAVA framework there used one group tag to club the repeated steps into one common step, is there anything like that we can use in cucumber c#?
CodePudding user response:
Group common steps into a scenario background:
Feature: ...
Background:
Given User Step1
And User Step2
And User Step3
And User Step4
And User Step5
Scenario: 1
When User Completes 5 steps in X
Then User Signoff X
Scenario: 2
When User Completes 5 steps in Y
Then User Signoff Y
Scenario: 3
When User Completes 5 steps in Z
Then User Signoff Z
The Background
steps will be executed for each scenario in the feature. If some scenarios should not include those steps then move them into their own feature file.
If all you do is save 5 steps per scenario, I would argue that you don't need to eliminate this repetition. The goal of behavior-driven development tests is to communicate the behavior, not eliminate code duplication.
My recommendation is to keep the repeated steps in each scenario if moving them into a Background
makes scenarios more difficult to understand. On the other hand, if moving the steps into a common Background
makes the scenarios easier to understand, then definitely move the common steps out of the scenarios.