Skip to content

Commit 1adb680

Browse files
committed
Add test_name_formatter
1 parent 363c1f6 commit 1adb680

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/pytest_mypy/__init__.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ def serialized(self) -> str:
5757
terminal_summary_title = "mypy"
5858

5959

60+
def default_test_name_formatter(*, item: MypyFileItem) -> str:
61+
path = item.path.relative_to(item.config.invocation_params.dir)
62+
return f"[{terminal_summary_title}] {path}"
63+
64+
65+
test_name_formatter = default_test_name_formatter
66+
67+
6068
def default_file_error_formatter(
6169
item: MypyItem,
6270
results: MypyResults,
@@ -268,13 +276,9 @@ def runtest(self) -> None:
268276
)
269277
)
270278

271-
def reportinfo(self) -> Tuple[str, None, str]:
279+
def reportinfo(self) -> Tuple[Path, None, str]:
272280
"""Produce a heading for the test report."""
273-
return (
274-
str(self.path),
275-
None,
276-
str(self.path.relative_to(self.config.invocation_params.dir)),
277-
)
281+
return (self.path, None, test_name_formatter(item=self))
278282

279283

280284
class MypyStatusItem(MypyItem):

tests/test_pytest_mypy.py

+23
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,29 @@ def pytest_configure(config):
342342
assert result.ret == pytest.ExitCode.OK
343343

344344

345+
def test_api_test_name_formatter(testdir, xdist_args):
346+
"""Ensure that the test_name_formatter can be replaced in a conftest.py."""
347+
test_name = "UnmistakableTestName"
348+
testdir.makepyfile(
349+
conftest=f"""
350+
cause_a_mypy_error: str = 5
351+
352+
def custom_test_name_formatter(item):
353+
return "{test_name}"
354+
355+
def pytest_configure(config):
356+
plugin = config.pluginmanager.getplugin('mypy')
357+
plugin.test_name_formatter = custom_test_name_formatter
358+
""",
359+
)
360+
result = testdir.runpytest_subprocess("--mypy", *xdist_args)
361+
result.stdout.fnmatch_lines([f"*{test_name}*"])
362+
mypy_file_check = 1
363+
mypy_status_check = 1
364+
result.assert_outcomes(failed=mypy_file_check + mypy_status_check)
365+
assert result.ret == pytest.ExitCode.TESTS_FAILED
366+
367+
345368
@pytest.mark.xfail(
346369
Version("0.971") <= MYPY_VERSION,
347370
raises=AssertionError,

0 commit comments

Comments
 (0)