Running Steps Inside Other Steps in behave

Tutorial​​ 9​​ –​​ Running Steps Inside Other Steps​​ in behave​​ 

Welcome to the 9th article in Behave BDD series!

What you will Learn :

  • Calling steps in other step definitions

  • context.execute_steps()

  • Another Example

Calling Steps in other step definitions

In programming, you might want to call/re-use one function inside another function. So basically, if we have a method that is already written in a function, we would not want to write the same method again in a new function.​​ The same technique we can use for steps as well.

In the previous tutorial, we saw how to share data between the steps. However, we were not able to use/share the variable defined in one scenario in another scenario. Let us see how to address this problem by calling the steps in other steps.

To understand this, let us look at the below feature file

context.execute_steps()

Now,​​ the problem in hand is, how do we share the variable that we define in first scenario with second scenario?

We can achieve this by implementing the below syntax in second scenario

So basically, we use context.execute_steps function and simply pass the steps from feature file that we want to execute in a scenario. This step is inclosed within unicode (u) Python string type. The Unicode lets Python programs work with all these different possible characters.

Now this is the step definition where we originally created the context variable order_num

Further this step def is mapped to below step in feature file (line number 3)

So, simply copy line number 3 from the feature file.

Next, inisde the step definition of scenario#2, we will call this step as seen below (we have simply pasted line number 3 in line​​ number 20). So, what will happen now is that, at runtime, the step def​​ corresponding to​​ ​​ line number 20 gets executed and we have the context.order_num = '123456' avaiable in second scenario as well

​​ 

Similarly​​ we do it for next step

Now, when we run the feature, notice that there is no error. The variable is avaiable in second scenario as well

Another Example

Look at the below feature. It has 2 scenarios.​​ The first scenario tests for a new user registration and the second scenario for an existing user

If you notice, the steps at line numbers 4-6 (in first scenario)​​ &​​ the step at line number 10 (in second scenario)​​ are​​ essentially doing the same thing​​ viz these steps fill the user details and submit. So what this means is that, we don’t have to create a new step definition for line number 10. Instead, we can simply call the step definitions for the steps at line numbers 4-6.

Let us​​ first write the step def for first scenario, see below

Next, let us write step def for line number 10

So​​ we start with creating @given step def and define a function

Next, we will add the below block

Next, go to the feature file and copy below steps

Next paste these inside the block

Save the file and let us run this much.

Notice below that the 3 steps are called from first scenario and we see the respective print outputs

Next we will add the last step definition as usual

Run the feature, notice both scenarios getting passed

So we have studied the core concept of ‘Running steps inside other steps’.

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024