pytest-bdd: Regression: KeyError in replacer

With #445, I’m seeing many tracebacks like this in my tests:

______________________ test_inserting_text_into_a_text_field_at_specific_position _______________________

request = <FixtureRequest for <Function test_inserting_text_into_a_text_field_at_specific_position>>
_pytest_bdd_example = {}

    @pytest.mark.usefixtures(*args)
    def scenario_wrapper(request, _pytest_bdd_example):
>       scenario = templated_scenario.render(_pytest_bdd_example)

.tox/bleeding/lib/python3.9/site-packages/pytest_bdd/scenario.py:173: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.tox/bleeding/lib/python3.9/site-packages/pytest_bdd/parser.py:249: in render
    steps = [
.tox/bleeding/lib/python3.9/site-packages/pytest_bdd/parser.py:251: in <listcomp>
    name=templated_step.render(context),
.tox/bleeding/lib/python3.9/site-packages/pytest_bdd/parser.py:364: in render
    return STEP_PARAM_RE.sub(replacer, self.name)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

m = <re.Match object; span=(18, 24), match='<Home>'>

    def replacer(m: typing.Match):
        varname = m.group(1)
>       return str(context[varname])
E       KeyError: 'Home'

.tox/bleeding/lib/python3.9/site-packages/pytest_bdd/parser.py:362: KeyError

There are many other such failures, but this one is from this scenario:

    Scenario: Inserting text into a text field at specific position
        When I open data/paste_primary.html
        And I insert "one two three four" into the text field
        And I run :click-element id qute-textarea
        And I wait for "Entering mode KeyMode.insert (reason: clicking input)" in the log
        # Move to the beginning and two characters to the right
        And I press the keys "<Home>"
        And I press the key "<Right>"
        And I press the key "<Right>"
        And I run :insert-text Hello world
        # Compare
        Then the javascript message "textarea contents: onHello worlde two three four" should be logged

specifically, the And I press the keys "<Home>" line there.

The underlying Python code is simple:

@bdd.when(bdd.parsers.re('I press the keys? "(?P<keys>[^"]*)"'))
def press_keys(quteproc, keys):
    """Send the given fake keys to qutebrowser."""
    quteproc.press_keys(keys)

A slightly simpler example:

    Scenario: :selection-follow with link tabbing (without JS)
        When I set content.javascript.enabled to false
        And I run :mode-leave
        And I run :jseval document.activeElement.blur();
        And I run :fake-key <tab>
        And I run :selection-follow
        Then data/hello.txt should be loaded

with this code:

@bdd.when(bdd.parsers.parse("I run {command}"))
def run_command(quteproc, server, tmpdir, command):
    # ...

results in a KeyError: tab. In other words, it seems like anything in <...> in a scenario now seems to be parsed in some special way.


I’ve tried to write a reproducer:

bug.feature:

Feature: Reproducer
    Scenario: Pressing keys
        When I run :fake-key <Ctrl+c>
        And I run :fake-key <tab>

test_bug.py:

import pytest_bdd as bdd

bdd.scenarios('bug.feature')

@bdd.when(bdd.parsers.parse("I run {command}"))
def run_command(command):
    pass

but unfortunately, I can not reproduce the issue there. Any ideas what could be going wrong there?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (10 by maintainers)

Commits related to this issue

Most upvoted comments

I’m fixing this in https://github.com/pytest-dev/pytest-bdd/pull/524 by parsing the params in angular brackets only for Scenario Outlines. Normal Scenarios will treat them verbatim.