Skip to content
Open
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
26 changes: 26 additions & 0 deletions plugin_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 6 additions & 8 deletions pytest_github_actions_annotate_failures/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -28,25 +27,24 @@
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):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""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

if lineno is not None:
# 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):
Expand Down