0

I have a test suite that used to be executed with pytest and I used the method before_all_tests(request) in each test file to initialize the db mockups for those tests.

I wanted to use pytest-xdist to run them parallelly, but before_all_tests(request) is not being executed if I run pytest -n X (parallelly).

Now I don't know how to re-write all my code so that I can only initialize the mocks of the tests being executed :(

I use markers to categorize tests, so sometimes I only run tests under a single category or just a bunch of them. I don't want to initialize the mocks of all the tests files because not all of them will be executed at that time.

Can anybody help me? I can post some code if necessary.

Thanks in advance!!

  • Are `before_all_tests` module-scoped fixtures? You can either run `xdist` using `--dist=loadfile` which will run tests in the same module together, or you can rewrite your fixtures to be function instead of module-scoped and only do the initialization if not already done. – MrBean Bremen May 27 '21 at 16:54
  • Thanks for your reply, @MrBeanBremen! Yes! `before_all_tests` is module-scoped but it doesn't get executed (I printed some dummy comments to see if it was being executed but it's not). I have it marked with `@pytest.fixture(scope="module", autouse=True)`. Is this correct? I tried running the pytest command with `--dist=loadfile` but it wasn't executed either. Thanks again! – Сабрина May 27 '21 at 19:50
  • 1
    The best way is probably the second one: make it a function-based fixture and use some global initialization flag to actually run the initialization only once. In this case it should not matter how the tests are exceuted by `xdist`. – MrBean Bremen May 27 '21 at 19:54

0 Answers0