Skip to content

Add test_name_formatter #192

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

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/pytest_mypy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_pytest_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down