Skip to content

Commit d4856d4

Browse files
Support Pytest 9+ subtests
Signed-off-by: Edgar Ramírez Mondragón <[email protected]>
1 parent e57cb85 commit d4856d4

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
@@ -351,6 +351,32 @@ def test_param(a, b):
351351
)
352352

353353

354+
@pytest.mark.skipif(
355+
version.parse("9.0.0") > PYTEST_VERSION,
356+
reason="subtests are only supported in pytest 9+",
357+
)
358+
def test_annotation_subtest(testdir: pytest.Testdir):
359+
testdir.makepyfile(
360+
"""
361+
import pytest
362+
pytest_plugins = 'pytest_github_actions_annotate_failures'
363+
364+
def test(subtests):
365+
for i in range(5):
366+
with subtests.test(msg="custom message", i=i):
367+
assert i % 2 == 0
368+
"""
369+
)
370+
testdir.monkeypatch.setenv("GITHUB_ACTIONS", "true")
371+
result = testdir.runpytest_subprocess()
372+
result.stderr.fnmatch_lines(
373+
[
374+
"::error file=test_annotation_subtest.py,line=7::test *custom message* *i=1*assert (1 %25 2) == 0*",
375+
"::error file=test_annotation_subtest.py,line=7::test *custom message* *i=3*assert (3 %25 2) == 0*",
376+
]
377+
)
378+
379+
354380
# Debugging / development tip:
355381
# Add a breakpoint() to the place you are going to check,
356382
# 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)