'Pytest xdist and parametrized tests is performing setup per each thread?

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 ?



Solution 1:[1]

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.

Solution 2:[2]

https://pytest-xdist.readthedocs.io/en/latest/how-to.html?highlight=only%20once#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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chanda Korat
Solution 2 Javier Buzzi