Behave: How to import steps from another file?
Asked Answered
M

2

6

I have just started using behave, a Pythonic BDD framework using Gherkin syntax. behave takes a feature, e.g.:

Scenario: Calling the metadata API
   Given A matching server
   When I call metadata
   Then metadata response is JSON
   And response status code is 200

And a steps file, e.g.:

...
@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

@then('metadata response is JSON')
def step_impl(context):
    json.loads(context.metadata_response.data)
...

And combines them to a beautiful test resport:

Test results

Some of these steps - like:

  • metadata response is JSON
  • response status code is {expected_status_code}

Are used in many of my projects, and I would like to group them into a general steps file which I can import and reuse.

I tried extracting useful steps to a separate file and importing it, but received the following error:

@then('response status code is {expected_status_code}')
NameError: name 'then' is not defined

How do I create a generic steps file and import it?

Moiety answered 3/2, 2014 at 9:25 Comment(0)
M
3

In the imported file, the behave decorators (like then) must be imported:

from behave import then
from nose.tools import assert_equals

@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

...
Moiety answered 3/2, 2014 at 9:25 Comment(2)
Amazingly, this answer has nothing to do with the question.Harmonize
Well, because it doesn't say anything about importing from another file... you are showing the file you import from, which is not really what question is about (it's about the file you import to).Harmonize
A
5

Just for all people out there, that are (like me) trying to import the single step-definition: Don't!

Simply import the whole module.

And here for all pepole (like me...) that still need more details:

If your project structure looks like this:

foo/bar.py
foo/behave/steps/bar_steps.py
foo/behave/bar.feature
foo/common_steps/baz.py

Just do

import foo.common_steps.baz

in foo/behave/steps/bar_steps.py (that is your regular step file)

Autophyte answered 24/5, 2016 at 11:36 Comment(0)
M
3

In the imported file, the behave decorators (like then) must be imported:

from behave import then
from nose.tools import assert_equals

@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

...
Moiety answered 3/2, 2014 at 9:25 Comment(2)
Amazingly, this answer has nothing to do with the question.Harmonize
Well, because it doesn't say anything about importing from another file... you are showing the file you import from, which is not really what question is about (it's about the file you import to).Harmonize

© 2022 - 2024 — McMap. All rights reserved.