Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subprocess coverage not set up correctly with xdist when passing --dist but not --numprocesses #667

Open
bloodearnest opened this issue Nov 15, 2024 · 0 comments

Comments

@bloodearnest
Copy link

bloodearnest commented 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:

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

Versions

Python 3.10

Package        Version
-------------- -------
coverage       7.6.7
exceptiongroup 1.2.2
execnet        2.1.1
iniconfig      2.0.0
packaging      24.2
pip            22.0.2
pluggy         1.5.0
pytest         8.3.3
pytest-cov     6.0.0
pytest-xdist   3.6.1
setuptools     59.6.0
tomli          2.1.0

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:

[tool.pytest.ini_options]
addopts = "--tb=native --strict-markers --dist=loadgroup"
@bloodearnest 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant