Pytest session scope fixture is not executed with xdist #8871
Replies: 1 comment
|
Hi @archana3112 With pytest-xdist, a session-scoped fixture does not mean once for the entire distributed test run. Each worker is its own pytest process/session, so a session fixture executes once per worker which needs that fixture. Also,
To verify execution, log something worker-specific or use @pytest.fixture(scope="session", autouse=True)
def setup(worker_id):
with open(f"/tmp/setup-{worker_id}.txt", "w") as f:
f.write("fixture ran")If the initialization really must occur exactly once across all xdist workers, that requires cross-process coordination (for example a shared file plus lock / xdist's documented shared-resource pattern), rather than ordinary Please mark this answer as accepted if it helped, thank you. |
Uh oh!
There was an error while loading. Please reload this page.
I am reading command line arguments using request.config.getoption in a setup fixture, setup fixture is defined in conftest.py
Setup fixture is to be executed at start of the session, so scope of fixture is 'session' and autouse is set to true. Every thing works fine with serial execution.
When I used xdist with loadscope for parallel execution, setup fixture is not executed. I read may issues on forum where people claims that with xdist session scope fixtures are executed multiple times, in my case fixture not executed at all. I added print statement inside fixture to monitor the execution. It is executed only when xdist is not used.
Please help if anyone came across similar issue. I am using python 3.7
All reactions