Skip to content

Commit 086ac10

Browse files
Support Pytest 9+ subtests
Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
1 parent c3aa5cc commit 086ac10

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

plugin_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,32 @@ def test_param(a, b):
327327
)
328328

329329

330+
@pytest.mark.skipif(
331+
version.parse("9.0.0") > PYTEST_VERSION,
332+
reason="subtests are only supported in pytest 9+",
333+
)
334+
def test_annotation_subtest(testdir: pytest.Testdir):
335+
testdir.makepyfile(
336+
"""
337+
import pytest
338+
pytest_plugins = 'pytest_github_actions_annotate_failures'
339+
340+
def test(subtests):
341+
for i in range(5):
342+
with subtests.test(msg="custom message", i=i):
343+
assert i % 2 == 0
344+
"""
345+
)
346+
testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true")
347+
result = testdir.runpytest_subprocess()
348+
result.stderr.fnmatch_lines(
349+
[
350+
"::error file=test_annotation_subtest.py,line=7::test *custom message* *i=1*assert (1 %25 2) == 0*",
351+
"::error file=test_annotation_subtest.py,line=7::test *custom message* *i=3*assert (3 %25 2) == 0*",
352+
]
353+
)
354+
355+
330356
# Debugging / development tip:
331357
# Add a breakpoint() to the place you are going to check,
332358
# uncomment this example, and run it with:

pytest_github_actions_annotate_failures/plugin.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
if TYPE_CHECKING:
1313
from warnings import WarningMessage
1414

15-
from _pytest.nodes import Item
16-
from _pytest.reports import CollectReport
15+
from _pytest.reports import TestReport
1716

1817

1918
# Reference:
@@ -28,25 +27,24 @@
2827
PYTEST_VERSION = version.parse(pytest.__version__)
2928

3029

31-
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
32-
def pytest_runtest_makereport(item: Item, call): # noqa: ARG001
33-
# execute all other hooks to obtain the report object
34-
outcome = yield
35-
report: CollectReport = outcome.get_result()
30+
@pytest.hookimpl(tryfirst=True)
31+
def pytest_runtest_logreport(report: TestReport):
32+
"""Handle test reporting for all pytest versions."""
3633

3734
# enable only in a workflow of GitHub Actions
3835
# ref: https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables
3936
if os.environ.get("GITHUB_ACTIONS") != "true":
4037
return
4138

39+
# Only handle failed tests in call phase
4240
if report.when == "call" and report.failed:
4341
filesystempath, lineno, _ = report.location
4442

4543
if lineno is not None:
4644
# 0-index to 1-index
4745
lineno += 1
4846

49-
longrepr = report.head_line or item.name
47+
longrepr = report.head_line or "test"
5048

5149
# get the error message and line number from the actual error
5250
if isinstance(report.longrepr, ExceptionRepr):

0 commit comments

Comments
 (0)