From 6e7a0b66f4fa2bdc3e75605b65f151c1fe70552c Mon Sep 17 00:00:00 2001 From: gabrii Date: Thu, 18 Sep 2025 12:27:02 +0200 Subject: [PATCH] Change how we run pytest, to increase coverage calculation --- app/commands.py | 6 ++---- tests/test_commnads.py | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/commands.py b/app/commands.py index 69a9d21..c6158fa 100644 --- a/app/commands.py +++ b/app/commands.py @@ -27,9 +27,7 @@ TEST_PATH = os.path.join(PROJECT_ROOT, "tests") ) def test(coverage, filter): """Run the tests.""" - import pytest - - args = [TEST_PATH, "--verbose"] + args = ["pytest", TEST_PATH, "--verbose"] if coverage: args.append("--cov=app") args.append("--cov-branch") @@ -38,7 +36,7 @@ def test(coverage, filter): args.append("--cov-report=term") if filter: args.extend(["-k", filter]) - rv = pytest.main(args=args) + rv = call(args) exit(rv) diff --git a/tests/test_commnads.py b/tests/test_commnads.py index 621d655..6908bce 100644 --- a/tests/test_commnads.py +++ b/tests/test_commnads.py @@ -6,14 +6,17 @@ import app.commands as commands def test_test_command_calls_pytest_with_coverage_and_exits(mocker): - """Invoke `test` command with defaults and ensure pytest args include coverage.""" - fake_main = mocker.patch("pytest.main", return_value=0) + """Invoke `test` command with defaults and ensure subprocess call args include coverage.""" + mock_call = mocker.patch("app.commands.call", return_value=0) runner = CliRunner() result = runner.invoke(commands.test) assert result.exit_code == 0 - expected = [ + mock_call.assert_called_once() + cmdline = mock_call.call_args[0][0] + assert cmdline == [ + "pytest", commands.TEST_PATH, "--verbose", "--cov=app", @@ -22,26 +25,25 @@ def test_test_command_calls_pytest_with_coverage_and_exits(mocker): "--cov-report=html", "--cov-report=term", ] - fake_main.assert_called_once() - assert fake_main.call_args.kwargs["args"] == expected def test_test_command_no_coverage_and_filter(mocker): - """Invoke `test` with no coverage and a filter; ensure pytest args are correct.""" - fake_main = mocker.patch("pytest.main", return_value=5) + """Invoke `test` with no coverage and a filter; ensure subprocess call args are correct.""" + mock_call = mocker.patch("app.commands.call", return_value=5) runner = CliRunner() result = runner.invoke(commands.test, ["-C", "-k", "unit and not e2e"]) assert result.exit_code == 5 - expected = [ + mock_call.assert_called_once() + cmdline = mock_call.call_args[0][0] + assert cmdline == [ + "pytest", commands.TEST_PATH, "--verbose", "-k", "unit and not e2e", ] - fake_main.assert_called_once() - assert fake_main.call_args.kwargs["args"] == expected def test_lint_command_invokes_tools_with_expected_order(mocker):