-
I have a repository whereby the majority of the tests are independent and using For that directory containing these order-dependent tests, I want to raise a warning if multiple workers are detected. For example, something like adding this to the directory's @pytest.fixture(scope="session", autouse=True)
def check_xdist() -> None:
if int(os.getenv("PYTEST_XDIST_WORKER_COUNT", "1")):
warnings.warn(
"Running the examples with multiple workers may cause issues. "
"Consider running the examples with a single worker."
) The problem with this solution is that when the test inevitably fails, the warning is buried below all the error messages that (most likely) inevitably are generated. I have considered replacing So my question is: Can I ensure that the ===================================== test session starts =====================================
platform darwin -- Python 3.13.1, pytest-8.3.4, pluggy-1.5.0
rootdir: /Users/joshua.ellis/src/pact-foundation/pact-python
configfile: pyproject.toml
plugins: asyncio-0.24.0, cov-6.0.0,nyio-4.7.0, bdd-8.1.0, xdist-3.6.1
asyncio: mode=Mode.STRICT, default_loop_scope=session
10 workers [18 items]
WARNING: Detected multiple xdist workers. Examples will most likely fail. <-- THIS
...EEEF...FF.F EDIT: to be clear, I do not want solutions which require addition command line arguments, as that would imply that the person calling pytest is already aware of the warning. I want the warning to be displayed when people are naively running pytest. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Implemented by the built-in plugin @pytest.fixture(scope='session', autouse=True)
def hello():
warnings.warn('hello')
|
Beta Was this translation helpful? Give feedback.
-
The solution ultimately was to use a hook. I can't speak for all hooks, but it would appear that PyTest's stdout and stderr capturing are not yet enabled, thereby making warnings like I have above print to the console. In my specific case of
|
Beta Was this translation helpful? Give feedback.
The solution ultimately was to use a hook. I can't speak for all hooks, but it would appear that PyTest's stdout and stderr capturing are not yet enabled, thereby making warnings like I have above print to the console.
In my specific case of
pytest-xdist
, they conveniently expose the hookpytest_xdist_setupnodes
which I can inspect to verify whether multiple workers are being used:conftest.py