pytest-dev / pytest-bdd

BDD library for the py.test runner
https://pytest-bdd.readthedocs.io/en/latest/
MIT License
1.27k stars 214 forks source link

Multiple step scenarios targeting the same fixture doesn't update the fixture #689

Open mawaliya opened 2 months ago

mawaliya commented 2 months ago

Env: Pytest 8.1.1 Pytest-bdd 7.1.2

more details

============================================================= test session starts =============================================================
platform linux -- Python 3.10.6, pytest-8.1.1, pluggy-1.5.0
cachedir: .pytest_cache
metadata: {'Python': '3.10.6', 'Platform': 'Linux-5.15.146.1-microsoft-standard-WSL2-x86_64-with-glibc2.35', 'Packages': {'pytest': '8.1.1', 'pluggy': '1.5.0'}, 'Plugins': {'metadata': '3.1.1', 'html': '4.1.1', 'retry': '1.6.2', 'syrupy': '4.6.1', 'bdd-html': '0.1.14a0', 'allure-pytest-bdd': '2.13.5', 'bdd': '7.1.2', 'check': '2.2.5'}, 'JAVA_HOME': '/usr/lib/jvm/java-17-openjdk-amd64'}

configfile: pyproject.toml
testpaths: tests
plugins: metadata-3.1.1, html-4.1.1, retry-1.6.2, syrupy-4.6.1, bdd-html-0.1.14a0, allure-pytest-bdd-2.13.5, bdd-7.1.2, check-2.2.5

Issue: when having multiple steps pointing to the same target_fixture, it's not getting updated, it seems it is only updated once when it is set for the first time. On pytest 7.4.2 and pytest-bdd 7.0.0 this works as expected

code to reproduce.

@when("it's AB", target_fixture="test")
def ab():
    return "AB"

@when("result is AB")
def res1(test):
    assert test == "AB"

@when("it's CD", target_fixture="test")
def cd():
    return "CD"

@when("result is CD")
def res2(test):
    assert test == "CD"
    Scenario: Test
        When it's AB
        When result is AB
        When it's CD
        When result is CD

I am expecting the scenario above to pass, but it will fail

    assert test == "CD"
E   AssertionError: assert 'AB' == 'CD'
E
E     - CD
E     + AB
martinwilkerson-ons commented 2 months ago

We are seeing this too, combinations tested that failed to behave as expected:

Downgrading to pytest 7.4.4 fixes it.

dcendents commented 1 month ago

I don't have time to open a pull request right now, will try to eventually but this seems to work (I modified the file locally in my venv and executed my BDD tests and they all pass with pytest-bdd 7.1.2 and pytest 8.2.0).

Changes between releases 7.0.1 and 7.1.2: https://github.com/pytest-dev/pytest-bdd/compare/7.0.1...7.1.2

file src/pytest_bdd/compat.py has been added with two methods to register a fixture, one for pytest 8.1+ and one for older pytest version.

I simply copied the inject_fixture function from pytest < 8.1 and adapted the arguments to create the FixtureDef with the correct arguments since pytest 8.1 and left the rest as is and it seems to work:

    def inject_fixture(request: FixtureRequest, arg: str, value: Any) -> None:
        """Inject fixture into pytest fixture request.

        :param request: pytest fixture request
        :param arg: argument name
        :param value: argument value
        """
        fd = FixtureDef(
            config=request._fixturemanager.config,
            baseid=request.node.nodeid,
            argname=arg,
            func=lambda: value,
            scope="function",
            params=None,
            ids=None,
            _ispytest=True,
        )

        fd.cached_result = (value, 0, None)

        old_fd = request._fixture_defs.get(arg)
        add_fixturename = arg not in request.fixturenames

        def fin() -> None:
            request._fixturemanager._arg2fixturedefs[arg].remove(fd)

            if old_fd is not None:
                request._fixture_defs[arg] = old_fd

            if add_fixturename:
                request._pyfuncitem._fixtureinfo.names_closure.remove(arg)

        request.addfinalizer(fin)

        # inject fixture definition
        request._fixturemanager._arg2fixturedefs.setdefault(arg, []).append(fd)

        # inject fixture value in request cache
        request._fixture_defs[arg] = fd
        if add_fixturename:
            request._pyfuncitem._fixtureinfo.names_closure.append(arg)

If someone wants to open a PR and make sure it doesn't break anything please feel free to do so. Otherwise I'll get to it eventually...

Cheers,

dcendents commented 1 month ago

I created the PR