'"How to execute a whole scenario with 5 steps before some particular scenarios in cucumber"

"Can i execute a specific scenario that is having suppose 5 steps before some specific scenaros in Cucumber?

Suppose i have a feature file that contains 3 scenarios say A,B and C.

I am writing another feature file and there are suppose 5 scenarios M,N,O,P and Q.

Now i want to execute scenario B before N and Q.

Note: Scenario B has multiple steps and i want to execute all the steps befor the execution of N and Q.

The @Before Hooks is applicable to single method and Background scenarios will execute before every senario. Please give some solution."



Solution 1:[1]

Calling another scenario is not supported in Cucumber-Java.

From the FAQ:

" Each scenario should be independent; you should be able to run them in any order or in parallel without one scenario interfering with another.

Each scenario should test exactly one thing so that when it fails, it fails for a clear reason. This means you wouldn’t reuse one scenario inside another scenario.

If your scenarios use the same or similar steps, or perform similar actions on your system, you can extract helper methods to do those things. "

From experience I can tell you that you really don't want your scenarios to depend on each other, because your automation will be very hard to maintain.

Solution 2:[2]

In Cucumber, every scenario should be independent. So, one option here is to keep the scenarios that have similar behavior in one feature file. In your case, I would keep scenarios B, N, Q in one feature file with B as Background.

Solution 3:[3]

You can achieve this with Cucumber TestNg where you would create another runner class where current runner would be dependent to it and give that as priority 0 and current runner priority as 1 and pass the tag 5 step scenario to 1st runner and next scenario tag to current runner.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marit
Solution 2 CMM
Solution 3 Sonali Das