Skip to content

Commit bdca2f3

Browse files
1 parent 42e8674 commit bdca2f3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/conftest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,44 @@
1+
import copy
2+
import os
3+
import sys
4+
from collections import OrderedDict
5+
from typing import TYPE_CHECKING
6+
17
import pytest
8+
9+
10+
if TYPE_CHECKING:
11+
from _pytest.reports import TestReport
12+
# Reference:
13+
# https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks
14+
# https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example
15+
# https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport
16+
#
17+
# Inspired by:
18+
# https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py
19+
20+
21+
@pytest.hookimpl(trylast=True)
22+
def pytest_runtest_logreport(report: "TestReport"):
23+
"""Add annotations of test failures or xpassed to github actions."""
24+
# enable only in a workflow of GitHub Actions
25+
if os.environ.get("GITHUB_ACTIONS") is None:
26+
return
27+
28+
if not report.when == "call":
29+
return
30+
31+
skip = not report.failed
32+
message = "Test Failure."
33+
if hasattr(report, "wasxfail") and report.outcome == "passed":
34+
skip = False
35+
message = "Unexpected test success."
36+
37+
if skip:
38+
return
39+
40+
print(
41+
"\n::error file={location[0]},line={location[1]},title={location[2]}::{message}".format(
42+
location=report.location, message=message
43+
)
44+
)

0 commit comments

Comments
 (0)