Skip to content

Pytest async method fixture is run with different self than the test method #633

@Guerilla94

Description

@Guerilla94

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

AnyIO version

4.0.0

Python version

3.11.4

What happened?

I'm trying to use an fixture class method to initialize some data for tests and store it in attributes. With synchronous fixture everything works as expected, the filled attributes are available in the test methods. But attributes filled in async function are not available in test methods. This happens because different objects are passed as "self" for the fixture and the test method.

I found similar issue for pytest-asyncio which was fixed

How can we reproduce the bug?

import pytest


class TestSomething:
    @pytest.fixture()
    async def do_setup(self):
        self.some_attr = 'value'

        print()
        print('in fixture')
        print(self)
        print(self.__dict__)

    async def test_something(self, do_setup):
        print()
        print('in test')
        print(self)          # different object
        print(self.__dict__) # some_attr missing

Output:

in fixture
<tests.test_anyio.TestSomething object at 0x107b0ca50>
{'some_attr': 'value'}

in test
<tests.test_anyio.TestSomething object at 0x107f06590>
{}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions