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

gh-633: fix fixture methods run with different self than the test method #804

Closed
wants to merge 6 commits into from

Conversation

graingert
Copy link
Collaborator

@graingert graingert commented Oct 11, 2024

Changes

Fixes #633 .

Checklist

If this is a user-facing code change, like a bugfix or a new feature, please ensure that
you've fulfilled the following conditions (where applicable):

  • You've added tests (in tests/) added which would fail without your patch
  • You've updated the documentation (in docs/, in case of behavior changes or new
    features)
  • You've added a new changelog entry (in docs/versionhistory.rst).

If this is a trivial change, like a typo fix or a code reformatting, then you can ignore
these instructions.

Updating the changelog

If there are no entries after the last release, use **UNRELEASED** as the version.
If, say, your patch fixes issue #123, the entry should look like this:

* Fix big bad boo-boo in task groups (#123 <https://github.com/agronholm/anyio/issues/123>_; PR by @yourgithubaccount)

If there's no issue linked, just link to your pull request instead by updating the
changelog after you've created the PR.

has_backend_arg = "anyio_backend" in fixturedef.argnames
original_func = fixturedef.func
fixturedef.func = wrapper
stack.callback(setattr, fixturedef, "func", original_func)
Copy link
Collaborator Author

@graingert graingert Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed so each request gets a rebound func to request.instance

this should also fix a case where anyio adopts a fixture and then subsequent tests use it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically the fixturedef persists across the whole test session, and you need to bind for each test run - so I monkeypatch fixturedef and then roll back the patches
this has a side effect of allowing other test plugins to adopt the fixture if they use the same strategy
I think my explanation is a little poor though
you may notice this approach is totally different from pytest-asyncio's approach. This is because I did not understand it

@graingert graingert marked this pull request as ready for review October 11, 2024 10:30
@agronholm
Copy link
Owner

Closing in favor of #807.

@agronholm agronholm closed this Oct 12, 2024
@agronholm agronholm deleted the fix-pytest-methods branch October 13, 2024 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pytest async method fixture is run with different self than the test method
2 participants