How to get the current behavior step with Python?

I use behavior with Python to run my tests. In the steps file I want to get the current step name, because if the test fails, I take a screenshot and rename the file to the step name.

Something like that:

Provided by the user logged in.
When the user does something.
Then something happens.

And I wanted my step code to be like this:

@given('user is logged in')  
try:  
   # MY CODE HERE  
except:  
   # GET THE STEP NAME HERE

Does anyone have an idea how to do this?

+4
source share
1 answer

I did what you are trying to do, take a screenshot of the failed test by setting the hook after_stepto my file environment.py.

def after_step(context, step):
    if step.status == "failed":
        take_the_shot(context.scenario.name + " " + step.name)

, , , .

, , behave step context , , scenario feature . before_step, , context.step.name. - :

def before_step(context, step):
    context.step = step

:

def step_impl(context):
    if some_condition:
        take_the_shot(context.scenario.name + " " + context.step.name)
+5

Source: https://habr.com/ru/post/1598902/


All Articles