Pass string to step definition (Behave BDD)

Tutorial​​ 17​​ ​​ ​​ Pass string to step definition​​ (Behave BDD)

Welcome to the 17th article in Behave BDD series!

What you will Learn :

  • Python debugger pdb

  • Pass string to step definition

  • Example 2​​ ​​ 

Add breakpoint in step definition

‘pdb’​​ is the python debugger. We can utilize it to add breakpoints​​ in step definition files.​​ To use the pdb​​ module, we have to first import it, see line number 2 below

Let us say we want to add a breakpoint @when (line#9) seen above. We can do that by adding the line​​ pdb.set_trace()​​ anywhere before @when, see below​​ 

Let us open the terminal and execute the feature file

Notice above that the script pauses at @when and we enter the (pdb) command prompt.

Type letter ‘l’ and hit enter, you would see the arrowmark pointing to @when​​ 

Pass string to step definition​​ file

Look at the below feature file. We have added a string just after ‘Given’ step. Let us see how to pass this string to step definition file

Let us first look at the step definition file, see below. Make sure to import the pdb module

Notice line number 8.​​ 

To access the string from feature file to step definition, we are capturing the output of context.text in a variable. We have also added a breakpoint.

Let us now execute the feature file by using the tagname, see below ​​​​ 


Notice the output of capture_text variable. It is the same string that we wrote in feature file. So this string is captured in the variable and we can use this string in our step definition file as desired.

Example 2​​ 

Let us now put another string, but this time in the form of an array, see below

Import json module in stepdefinition file

Capture json data in my_array variable, also add a breakpoint, see below

Let us now run the feature file

json.loads(context.text)​​ returns us the json dictionary (having key:value pairs coming from feature file).

We can now print the value of any key, example ‘username’.​​ 

So my_array[‘username’] returns the respective value ‘testuser1’.

We can now use these values as desired in our step definition file.

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