How to get the current behave step with Python?

Lucas Mello

I'm using behave with Python to do my tests. In the step 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 this:

Given user is logged in
When 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

Anyone have an idea how to do this?

Louis

I've done essentially what you are trying to do, take a screenshot of a failed test, by setting an after_step hook in my environment.py file.

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

You most likely want the scenario name in there too if a step can appear in multiple scenarios.

If the above does not work for you, unfortunately behave does not set a step field on context with the current step like it does for scenario with the current scenario or feature with the current feature. You could have a before_step hook that performs such assignment and then in the step itself, you'd access the step's name as context.step.name. Something like:

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

And the step implementation:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the current behave step with Python?

From Dev

How to get the current executing step information in Specflow

From Dev

How can I specify the type of a column in a python behave step data table?

From Dev

How is data from one Behave step passed to a later step?

From Dev

How to get result of "reduce" at every step in Python?

From Java

How to get the current time in Python

From Dev

TDD Implementing Step Definitions derived from Behave (Python)

From Dev

Skip a behave step in the step implementation

From Dev

How to login user during Given step using django-behave

From Dev

Python: How to integrate behave into pytest?

From Dev

How can I get the distance, direction, duration from my current location to next step?

From Dev

How to get current URL from Mechanize in Python?

From Dev

How to get the current Linux theme with Python?

From Dev

How to get the current script's code in Python?

From Dev

How to get current URL from Mechanize in Python?

From Dev

How to get the current Linux theme with Python?

From Dev

How can I get code coverage data from Python BDD functional tests using Behave?

From Dev

How to get DateCompare() to behave in ColdFusion 10?

From Dev

How to get Django ForeignKey to behave like a DateField

From Dev

Cucumber JS Get current Feature / Scenario / Step in World

From Java

How to get current route

From Dev

How to get current InputMethodService

From Dev

How get current timezone?

From Dev

How to get current month?

From Dev

How to get current path?

From Dev

How to get Spotify current playing song in Python (Windows)?

From Java

How to get the current working directory using python 3?

From Dev

Python eve - how to get the current user after successful authentication?

From Dev

How to use python to get the current commit of its own github repository

Related Related

  1. 1

    How to get the current behave step with Python?

  2. 2

    How to get the current executing step information in Specflow

  3. 3

    How can I specify the type of a column in a python behave step data table?

  4. 4

    How is data from one Behave step passed to a later step?

  5. 5

    How to get result of "reduce" at every step in Python?

  6. 6

    How to get the current time in Python

  7. 7

    TDD Implementing Step Definitions derived from Behave (Python)

  8. 8

    Skip a behave step in the step implementation

  9. 9

    How to login user during Given step using django-behave

  10. 10

    Python: How to integrate behave into pytest?

  11. 11

    How can I get the distance, direction, duration from my current location to next step?

  12. 12

    How to get current URL from Mechanize in Python?

  13. 13

    How to get the current Linux theme with Python?

  14. 14

    How to get the current script's code in Python?

  15. 15

    How to get current URL from Mechanize in Python?

  16. 16

    How to get the current Linux theme with Python?

  17. 17

    How can I get code coverage data from Python BDD functional tests using Behave?

  18. 18

    How to get DateCompare() to behave in ColdFusion 10?

  19. 19

    How to get Django ForeignKey to behave like a DateField

  20. 20

    Cucumber JS Get current Feature / Scenario / Step in World

  21. 21

    How to get current route

  22. 22

    How to get current InputMethodService

  23. 23

    How get current timezone?

  24. 24

    How to get current month?

  25. 25

    How to get current path?

  26. 26

    How to get Spotify current playing song in Python (Windows)?

  27. 27

    How to get the current working directory using python 3?

  28. 28

    Python eve - how to get the current user after successful authentication?

  29. 29

    How to use python to get the current commit of its own github repository

HotTag

Archive