How to run setup (fixture) once and then tests in parallel

3.3k views Asked by At

I was using pytest-xdist to run tests in parallel, but my suite setup is very huge and bulky, which I want to avoid running multiple times in each tests execution.
While I was using pytest-xdist to run all the tests in parallel, I came across the problem where my suite setup (fixtures) are running before every test execution which increases the execution time of all the test cases.
Is there any way to avoid the fixture execution before every test execution in pytest parallel execution?

1

There are 1 answers

0
joliver On BEST ANSWER

Specifying a session scope for your fixtures would usually be the way to go, if you were not using pytest-xdist. Fixtures with session scope won't work as expected when you're using pytest-xdist, because the tests are being executed in separate processes.

It is a known limitation of pytest-xdist: https://pytest-xdist.readthedocs.io/en/stable/how-to.html#making-session-scoped-fixtures-execute-only-once

If it's data that you can store in a file, for example, there is a suggested workaround in the documentation. If it's a process that you need to run first, then I'm afraid you may have to move this outside of your tests.

There are open issues on their GitHub repo concerning this issue, such as this one: https://github.com/pytest-dev/pytest-xdist/issues/271