Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit cfade87

Browse files
feat: let users specify pytest command (#322)
context: codecov/engineering-team#715 Apparently some users don't have a virtualenv or don't install packages globaly. In this case running `python -m pytest` doesn't work because pytest is not accessible to the global python. These changes let users specify a different command to run, so that they can tell the PytestStandardRunner where to find `pytest`. In terms of security it's not more dangerous than having a DAN runner, so should be ok.
1 parent ce47206 commit cfade87

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

codecov_cli/runners/pytest_standard_runner.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616

1717

1818
class PytestStandardRunnerConfigParams(dict):
19+
@property
20+
def pytest_command(self) -> List[str]:
21+
command_from_config = self.get("pytest_command")
22+
if isinstance(command_from_config, str):
23+
logger.warning("pytest_command should be a list")
24+
command_from_config = command_from_config.split(" ")
25+
return command_from_config or ["python", "-m", "pytest"]
26+
1927
@property
2028
def collect_tests_options(self) -> List[str]:
2129
return self.get("collect_tests_options", [])
@@ -62,7 +70,7 @@ def _execute_pytest(self, pytest_args: List[str], capture_output: bool = True):
6270
Raises Exception if pytest fails
6371
Returns the complete pytest output
6472
"""
65-
command = ["python", "-m", "pytest"] + pytest_args
73+
command = self.params.pytest_command + pytest_args
6674
try:
6775
result = subprocess.run(
6876
command,
@@ -92,7 +100,10 @@ def collect_tests(self):
92100
logger.info(
93101
"Collecting tests",
94102
extra=dict(
95-
extra_log_attributes=dict(pytest_options=options_to_use),
103+
extra_log_attributes=dict(
104+
pytest_command=self.params.pytest_command,
105+
pytest_options=options_to_use,
106+
),
96107
),
97108
)
98109

tests/runners/test_pytest_standard_runner.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ def test_execute_pytest(self, mock_subprocess):
3939
)
4040
assert result == output
4141

42+
@pytest.mark.parametrize(
43+
"command_configured", [["pyenv", "pytest"], "pyenv pytest"]
44+
)
45+
@patch("codecov_cli.runners.pytest_standard_runner.subprocess")
46+
def test_execute_pytest_user_provided_command(
47+
self, mock_subprocess, command_configured
48+
):
49+
output = "Output in stdout"
50+
return_value = MagicMock(stdout=output.encode("utf-8"))
51+
mock_subprocess.run.return_value = return_value
52+
53+
runner = PytestStandardRunner(dict(pytest_command=command_configured))
54+
55+
result = runner._execute_pytest(["--option", "--ignore=batata"])
56+
mock_subprocess.run.assert_called_with(
57+
["pyenv", "pytest", "--option", "--ignore=batata"],
58+
capture_output=True,
59+
check=True,
60+
stdout=None,
61+
)
62+
assert result == output
63+
4264
@patch("codecov_cli.runners.pytest_standard_runner.subprocess")
4365
def test_execute_pytest_fail_collection(self, mock_subprocess):
4466
def side_effect(command, *args, **kwargs):

0 commit comments

Comments
 (0)