step definition is not found -- pytest-bdd

1.8k views Asked by At

I'm trying to set up a pytest-bdd suite for the following feature.

Feature: Tree

# BUILD TOP-DOWN 

Scenario: Add properties to tree
  Given a new tree is created with name default
  When the name of the tree is asked for
  Then default is returned

For some reason, my when step is not being recognized, though the strings appear identical:

from pytest_bdd import scenario, given, when, then

from models import Tree

@scenario("../features/Tree.feature", "Add properties to tree")
def test_tree():
    pass

@given("a new tree is created with name default", target_fixture="tree")
def tree():
    return Tree(name="default")

@when("the name of the tree is asked for")
def get_name(tree):
    return tree.get_name()

@then("default is returned")
def is_default(get_name):
    assert test_get_name == 'default'

I get the following error message when I run this:

(sorry about the length. The relevant part appears to be "Step definition is not found: When "the name of the tree is asked for".

========================================================================================== FAILURES ==========================================================================================
_________________________________________________________________________________________ test_tree __________________________________________________________________________________________

self = <FixtureRequest for <Function test_tree>>, argname = 'pytestbdd_when_the name of the tree is asked for'

    def _get_active_fixturedef(self, argname):
        try:
>           return self._fixture_defs[argname]
E           KeyError: 'pytestbdd_when_the name of the tree is asked for'

/usr/local/lib/python3.7/site-packages/_pytest/fixtures.py:491: KeyError

During handling of the above exception, another exception occurred:

request = <FixtureRequest for <Function test_tree>>, step = <pytest_bdd.feature.Step object at 0x108132b50>, scenario = <pytest_bdd.feature.Scenario object at 0x107885d10>
encoding = 'utf-8'

    def _find_step_function(request, step, scenario, encoding):
        """Match the step defined by the regular expression pattern.
    
        :param request: PyTest request object.
        :param step: Step.
        :param scenario: Scenario.
    
        :return: Function of the step.
        :rtype: function
        """
        name = step.name
        try:
            # Simple case where no parser is used for the step
>           return request.getfixturevalue(get_step_fixture_name(name, step.type, encoding))

/usr/local/lib/python3.7/site-packages/pytest_bdd/scenario.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <FixtureRequest for <Function test_tree>>, argname = 'pytestbdd_when_the name of the tree is asked for'

    def getfixturevalue(self, argname):
        """ Dynamically run a named fixture function.
    
        Declaring fixtures via function argument is recommended where possible.
        But if you can only decide whether to use another fixture at test
        setup time, you may use this function to retrieve it inside a fixture
        or test function body.
        """
>       return self._get_active_fixturedef(argname).cached_result[0]

/usr/local/lib/python3.7/site-packages/_pytest/fixtures.py:487: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <FixtureRequest for <Function test_tree>>, argname = 'pytestbdd_when_the name of the tree is asked for'

    def _get_active_fixturedef(self, argname):
        try:
            return self._fixture_defs[argname]
        except KeyError:
            try:
>               fixturedef = self._getnextfixturedef(argname)

/usr/local/lib/python3.7/site-packages/_pytest/fixtures.py:494: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <FixtureRequest for <Function test_tree>>, argname = 'pytestbdd_when_the name of the tree is asked for'

    def _getnextfixturedef(self, argname):
        fixturedefs = self._arg2fixturedefs.get(argname, None)
        if fixturedefs is None:
            # we arrive here because of a dynamic call to
            # getfixturevalue(argname) usage which was naturally
            # not known at parsing/collection time
            parentid = self._pyfuncitem.parent.nodeid
            fixturedefs = self._fixturemanager.getfixturedefs(argname, parentid)
            self._arg2fixturedefs[argname] = fixturedefs
        # fixturedefs list is immutable so we maintain a decreasing index
        index = self._arg2index.get(argname, 0) - 1
        if fixturedefs is None or (-index > len(fixturedefs)):
>           raise FixtureLookupError(argname, self)
E           _pytest.fixtures.FixtureLookupError: ('pytestbdd_when_the name of the tree is asked for', <FixtureRequest for <Function test_tree>>)

/usr/local/lib/python3.7/site-packages/_pytest/fixtures.py:393: FixtureLookupError

During handling of the above exception, another exception occurred:

request = <FixtureRequest for <Function test_tree>>

    @pytest.mark.usefixtures(*function_args)
    def scenario_wrapper(request):
>       _execute_scenario(feature, scenario, request, encoding)

/usr/local/lib/python3.7/site-packages/pytest_bdd/scenario.py:179: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/local/lib/python3.7/site-packages/pytest_bdd/scenario.py:139: in _execute_scenario
    step_func = _find_step_function(request, step, scenario, encoding=encoding)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

request = <FixtureRequest for <Function test_tree>>, step = <pytest_bdd.feature.Step object at 0x108132b50>, scenario = <pytest_bdd.feature.Scenario object at 0x107885d10>
encoding = 'utf-8'

    def _find_step_function(request, step, scenario, encoding):
        """Match the step defined by the regular expression pattern.
    
        :param request: PyTest request object.
        :param step: Step.
        :param scenario: Scenario.
    
        :return: Function of the step.
        :rtype: function
        """
        name = step.name
        try:
            # Simple case where no parser is used for the step
            return request.getfixturevalue(get_step_fixture_name(name, step.type, encoding))
        except pytest_fixtures.FixtureLookupError:
            try:
                # Could not find a fixture with the same name, let's see if there is a parser involved
                name = find_argumented_step_fixture_name(name, step.type, request._fixturemanager, request)
                if name:
                    return request.getfixturevalue(name)
                raise
            except pytest_fixtures.FixtureLookupError:
                raise exceptions.StepDefinitionNotFoundError(
                    u"""Step definition is not found: {step}."""
                    """ Line {step.line_number} in scenario "{scenario.name}" in the feature "{feature.filename}""".format(
>                       step=step, scenario=scenario, feature=scenario.feature
                    )
                )
E               pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: When "the name of the tree is asked for". Line 7 in scenario "Add properties to tree" in the feature "/Users/davidjoseph/projects/polythought/tests/features/Tree.feature

/usr/local/lib/python3.7/site-packages/pytest_bdd/scenario.py:88: StepDefinitionNotFoundError
================================================================================== short test summary info ===================================================================================
FAILED tests/step_defs/test_tree_steps.py::test_tree - pytest_bdd.exceptions.StepDefinitionNotFoundError: Step definition is not found: When "the name of the tree is asked for". Line 7 in...
ERROR tests/step_defs/test_tree_steps.py::test_get_name
0

There are 0 answers