pytest-dev / pytest-bdd

BDD library for the pytest runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.32k stars 221 forks source link

Using the same names for parameters in BDD steps results in a conflict #438

Closed DmitryKorolev-TomTom closed 2 years ago

DmitryKorolev-TomTom commented 3 years ago

If multiple steps are using the same parameter name, the first occurrence will populate the fixture value and that value will be used in all successive occurrences. I will illustrate it on the example:

  Scenario Outline: example
    When I apply ABC
    Then the status is <status>
    Examples:
      | status |
      | XYZ    |
@when(parsers.parse("I apply {status}"))
def apply(status):
    pass

@then(parsers.parse("the status is <status>"))
def check(status):
    assert_that(status, equal_to("XYZ"))

My expectation here is that in then step value for status will be populated from the example table. However, this is not the case as this test fails with:

assert_that(status, equal_to("XYZ"))
AssertionError:
Expected: 'XYZ'
     but: was 'ABC'
jirikuncar commented 3 years ago

My understanding is that something around these lines should work for you:

@pytest.fixture
def ctx():
    return {}

@when(parsers.parse("I apply {status}"))
def apply(ctx, status):
    ctx["status"] = status

@then(parsers.parse("the status is <status>"))
def check(ctx, status):
    assert_that(status, equal_to(ctx["status"]))
elchupanebrej commented 3 years ago

Duplicate of #412

olegpidsadnyi commented 2 years ago

@DmitryKorolev-TomTom is this still a case since the refactoring? Now the scenario renders before matching the step and all steps should use {status} to parse it. Please check with the latest and close the issue if it works.

elchupanebrej commented 2 years ago

There is a possibility to dissociate usage of step parameters and fixtures: https://github.com/elchupanebrej/pytest-bdd-ng#step-arguments-are-fixtures-as-well

youtux commented 2 years ago

Closing this as this should have been fixed in the latest versions of pytest-bdd. Please retry with the latest version of pytest-bdd, I will reopen the issue if the problem is still present.