You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When any of --numprocesses, --distload, or --dist cli args are present, pytest-cov selects the DistMaster controller for the main pytest process, assuming xdist is being used:
Unlike the Central or DistWorker controllers, the DistMaster does not call set_env() on the current process, instead relying on DistWorker controller to set it in the xdist worker process.
However, unless --numprocesses is passed, xdist doesn't actually run the tests using worker subprocesses, but runs it in the current pytest processes. This doesn't have the env vars set up for subprocess coverage to work.
For example, if you add --dist=loadgroup to your default pytest args in pyproject.toml, then subprocess coverage will only work if you also run with -n.
Perhaps only --numprocesses should trigger using DistMaster? Or maybe DistMaster should also set the env vars, in case --numprocess is not used but --dist is, like in our case?
Expected vs actual result
Expected: Subprocess coverage works when using --dist but not -n.
Actual: Subprocess coverage doesn't work if you pass --dist but not -n
Reproducer
Given this tests.py:
import os
def test_subprocess_env_vars():
assert "COV_CORE_SOURCE" in os.environ
Tests:
This passes: pytest --cov=. tests.py
This passes: pytest --cov=. -n 1 tests.py
This does not pass: pytest --cov=. --dist=loadgroup tests.py
The text was updated successfully, but these errors were encountered:
bloodearnest
changed the title
pytest-cov/xdist subprocess not set up correctly when passing --dist but not --numprocesses
Subprocess coverage not set up correctly with xdist when passing --dist but not --numprocesses
Nov 15, 2024
Summary
When any of
--numprocesses
,--distload
, or--dist
cli args are present, pytest-cov selects the DistMaster controller for the main pytest process, assuming xdist is being used:https://github.com/pytest-dev/pytest-cov/blob/master/src/pytest_cov/plugin.py#L221
Unlike the Central or DistWorker controllers, the DistMaster does not call
set_env()
on the current process, instead relying on DistWorker controller to set it in the xdist worker process.However, unless
--numprocesses
is passed, xdist doesn't actually run the tests using worker subprocesses, but runs it in the current pytest processes. This doesn't have the env vars set up for subprocess coverage to work.For example, if you add
--dist=loadgroup
to your default pytest args in pyproject.toml, then subprocess coverage will only work if you also run with-n
.Perhaps only
--numprocesses
should trigger using DistMaster? Or maybe DistMaster should also set the env vars, in case--numprocess
is not used but --dist is, like in our case?Expected vs actual result
Expected: Subprocess coverage works when using --dist but not -n.
Actual: Subprocess coverage doesn't work if you pass --dist but not -n
Reproducer
Given this tests.py:
Tests:
This passes:
pytest --cov=. tests.py
This passes:
pytest --cov=. -n 1 tests.py
This does not pass:
pytest --cov=. --dist=loadgroup tests.py
Versions
Python 3.10
Also tested with python 3.11, pytest-cov 5.0.0.
Config
None needed as reproducible w/o any config. However, this was discovered because we had the following in pyproject.toml:
The text was updated successfully, but these errors were encountered: