2
votes

I have a test file that I run with pytest

The file has parametrized tests and also a setup that I want it to run only once before any of the tests run, on that setup I do actions that can't be done in parallel (Write to text files).

That setup is like this

@pytest.fixture(scope="session", autouse=True)
def before_all_tests(request):
# Code that I want to run only once before all tests start to run

When I run with no -n parameter everything works OK

When I run with -n then I have issues that I understand that are because two threads are doing the setup at the same time

Is this the way xdist works ? The setup is doing per thread ? Is there any scope that ensures me that the setup is doing only once before each thread starts to run tests ?

2

2 Answers

2
votes

Is this the way xdist works ? The setup is doing per thread ?

Yes. Sesion scoped fixtures are completely supported within a single process (one global fixture when xdist isn't used, otherwise one fixture per xdist worker)

Unfortunately, pytest doesn't support sharing fixtures across xdist workers. Here's the issue reported for the same.

1
votes

https://github.com/pytest-dev/pytest-xdist#making-session-scoped-fixtures-execute-only-once recommends to write the shared resource to a file and put a lock on it to prevent re-executing the code.