Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mawaliya opened this issue Apr 26, 2024 · 3 comments · May be fixed by #690
Open

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

mawaliya opened this issue Apr 26, 2024 · 3 comments · May be fixed by #690
Labels

Comments

@mawaliya
Copy link

mawaliya commented Apr 26, 2024

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
Copy link

martinwilkerson-ons commented Apr 30, 2024

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

  • pytest 8.1.2, pytest-bdd 7.1.2
  • pytest 8.2.0, pytest-bdd 7.1.2

Downgrading to pytest 7.4.4 fixes it.

@dcendents
Copy link
Contributor

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: 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,

@youtux youtux added the bug label May 10, 2024
@dcendents dcendents linked a pull request May 10, 2024 that will close this issue
@dcendents
Copy link
Contributor

I created the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants