pytest-dev / pytest-xdist

pytest plugin for distributed testing and loop-on-failures testing modes.
https://pytest-xdist.readthedocs.io
MIT License
1.45k stars 231 forks source link

`pytest_configure` is ignored in `conftest.py` when using xdist #917

Closed ericpre closed 5 months ago

ericpre commented 1 year ago

When running the test suite with pytest --pyargs package -n 2, the content of the pytest_configure hook in conftest.py doesn't run:

def pytest_configure(config):
    # Run in pytest_configure hook to avoid capturing stdout by pytest and
    # inform user that the test data are being downloaded

    # Workaround to avoid running it for each worker
    worker_id = os.environ.get("PYTEST_XDIST_WORKER")
    if worker_id is None:
        print("Checking if test data need downloading...")
        download_all()
        print("All test data available.")

While it works fine when running pytest -n 2 from the tests folder.

lesteve commented 6 months ago

I have bumped into this as well, this is not that pytest_configure is ignored but that it is only run in the xdist workers and not in the xdist controller when using pytest --pyargs for some reason.

This can be an issue when setting up something in the xdist controller and accessing it in the xdist workers as in https://github.com/pytest-dev/pytest/issues/1402#issuecomment-186299177. In my case I want to set-up and random generator seed in the xdist controller and reuse it in the xdist workers to control random number generation.

If I get the time I'll try to put together a stand-alone example reproducing the issue.

RonnyPfannschmidt commented 6 months ago

this is expected - without collection only initial conftests are considered

as far as i am aware pyargs are not considered correctly for initial contests

i suspect this worked for you before by accident

i recommend using a autouse fixture and using something like https://pytest-xdist.readthedocs.io/en/latest/how-to.html#making-session-scoped-fixtures-execute-only-once to ensure it only happens once

lesteve commented 5 months ago

OK thanks for your answer, I will need to have a closer look. I need the shared random seed in pytest_generate_tests and I am not sure how I can do this with the autouse fixture yet ...

ericpre commented 5 months ago

Thank you @RonnyPfannschmidt for the recommendation, it does work as expected!