From 1adb680fdfe0272cabfb11f6ee84ac58fb2e4107 Mon Sep 17 00:00:00 2001 From: David Tucker Date: Wed, 12 Mar 2025 00:07:34 -0700 Subject: [PATCH] Add test_name_formatter --- src/pytest_mypy/__init__.py | 16 ++++++++++------ tests/test_pytest_mypy.py | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/pytest_mypy/__init__.py b/src/pytest_mypy/__init__.py index af33ec1..4163b71 100644 --- a/src/pytest_mypy/__init__.py +++ b/src/pytest_mypy/__init__.py @@ -57,6 +57,14 @@ def serialized(self) -> str: terminal_summary_title = "mypy" +def default_test_name_formatter(*, item: MypyFileItem) -> str: + path = item.path.relative_to(item.config.invocation_params.dir) + return f"[{terminal_summary_title}] {path}" + + +test_name_formatter = default_test_name_formatter + + def default_file_error_formatter( item: MypyItem, results: MypyResults, @@ -268,13 +276,9 @@ def runtest(self) -> None: ) ) - def reportinfo(self) -> Tuple[str, None, str]: + def reportinfo(self) -> Tuple[Path, None, str]: """Produce a heading for the test report.""" - return ( - str(self.path), - None, - str(self.path.relative_to(self.config.invocation_params.dir)), - ) + return (self.path, None, test_name_formatter(item=self)) class MypyStatusItem(MypyItem): diff --git a/tests/test_pytest_mypy.py b/tests/test_pytest_mypy.py index d4dd6a9..e217f2d 100644 --- a/tests/test_pytest_mypy.py +++ b/tests/test_pytest_mypy.py @@ -342,6 +342,29 @@ def pytest_configure(config): assert result.ret == pytest.ExitCode.OK +def test_api_test_name_formatter(testdir, xdist_args): + """Ensure that the test_name_formatter can be replaced in a conftest.py.""" + test_name = "UnmistakableTestName" + testdir.makepyfile( + conftest=f""" + cause_a_mypy_error: str = 5 + + def custom_test_name_formatter(item): + return "{test_name}" + + def pytest_configure(config): + plugin = config.pluginmanager.getplugin('mypy') + plugin.test_name_formatter = custom_test_name_formatter + """, + ) + result = testdir.runpytest_subprocess("--mypy", *xdist_args) + result.stdout.fnmatch_lines([f"*{test_name}*"]) + mypy_file_check = 1 + mypy_status_check = 1 + result.assert_outcomes(failed=mypy_file_check + mypy_status_check) + assert result.ret == pytest.ExitCode.TESTS_FAILED + + @pytest.mark.xfail( Version("0.971") <= MYPY_VERSION, raises=AssertionError,