diff --git a/plugin_test.py b/plugin_test.py index c52315a..5231b29 100644 --- a/plugin_test.py +++ b/plugin_test.py @@ -351,6 +351,32 @@ def test_param(a, b): ) +@pytest.mark.skipif( + version.parse("9.0.0") > PYTEST_VERSION, + reason="subtests are only supported in pytest 9+", +) +def test_annotation_subtest(testdir: pytest.Testdir): + testdir.makepyfile( + """ + import pytest + pytest_plugins = 'pytest_github_actions_annotate_failures' + + def test(subtests): + for i in range(5): + with subtests.test(msg="custom message", i=i): + assert i % 2 == 0 + """ + ) + testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true") + result = testdir.runpytest_subprocess() + result.stderr.fnmatch_lines( + [ + "::error file=test_annotation_subtest.py,line=7::test *custom message* *i=1*assert (1 %25 2) == 0*", + "::error file=test_annotation_subtest.py,line=7::test *custom message* *i=3*assert (3 %25 2) == 0*", + ] + ) + + # Debugging / development tip: # Add a breakpoint() to the place you are going to check, # uncomment this example, and run it with: diff --git a/pytest_github_actions_annotate_failures/plugin.py b/pytest_github_actions_annotate_failures/plugin.py index bc18968..fc493eb 100644 --- a/pytest_github_actions_annotate_failures/plugin.py +++ b/pytest_github_actions_annotate_failures/plugin.py @@ -12,8 +12,7 @@ if TYPE_CHECKING: from warnings import WarningMessage - from _pytest.nodes import Item - from _pytest.reports import CollectReport + from _pytest.reports import TestReport # Reference: @@ -28,17 +27,16 @@ PYTEST_VERSION = version.parse(pytest.__version__) -@pytest.hookimpl(tryfirst=True, hookwrapper=True) -def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 - # execute all other hooks to obtain the report object - outcome = yield - report: CollectReport = outcome.get_result() +@pytest.hookimpl(tryfirst=True) +def pytest_runtest_logreport(report: TestReport): + """Handle test reporting for all pytest versions.""" # enable only in a workflow of GitHub Actions # ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables if os.environ.get("GITHUB_ACTIONS") != "true": return + # Only handle failed tests in call phase if report.when == "call" and report.failed: filesystempath, lineno, _ = report.location @@ -46,7 +44,7 @@ def pytest_runtest_makereport(item: Item, call): # noqa: ARG001 # 0-index to 1-index lineno += 1 - longrepr = report.head_line or item.name + longrepr = report.head_line or "test" # get the error message and line number from the actual error if isinstance(report.longrepr, ExceptionRepr):