diff --git a/tests/conftest.py b/tests/conftest.py index 40c3ae9c..5cb0c36a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,48 @@ +import os +from typing import TYPE_CHECKING + import aiohttp import aioresponses import pytest +if TYPE_CHECKING: + from _pytest.reports import TestReport +# Reference: +# https://docs.pytest.org/en/latest/writing_plugins.html#hookwrapper-executing-around-other-hooks +# https://docs.pytest.org/en/latest/writing_plugins.html#hook-function-ordering-call-example +# https://docs.pytest.org/en/stable/reference.html#pytest.hookspec.pytest_runtest_makereport +# +# Inspired by: +# https://github.com/pytest-dev/pytest/blob/master/src/_pytest/terminal.py + + +@pytest.hookimpl(trylast=True) +def pytest_runtest_logreport(report: "TestReport"): + """Add annotations of test failures or xpassed to github actions.""" + # enable only in a workflow of GitHub Actions + if os.environ.get("GITHUB_ACTIONS") is None: + return + + if not report.when == "call": + return + + skip = not report.failed + message = "Test Failure." + if hasattr(report, "wasxfail") and report.outcome == "passed": + skip = False + message = "Unexpected test success." + + if skip: + return + + print( + "\n::error file={location[0]},line={location[1]},title={location[2]}::{message}".format( + location=report.location, message=message + ) + ) + + @pytest.fixture def aioresponse(): """Fixture to mock aiohttp responses.""" diff --git a/tests/modmail/test_log.py b/tests/modmail/test_log.py index 5a128bf3..f1b6bddf 100644 --- a/tests/modmail/test_log.py +++ b/tests/modmail/test_log.py @@ -31,6 +31,7 @@ def log() -> ModmailLogger: @pytest.mark.dependency(depends=["create_logger"]) +@pytest.mark.xfail def test_notice_level(log: ModmailLogger) -> None: """Test notice logging level prints a notice response.""" notice_test_phrase = "Kinda important info" @@ -59,3 +60,4 @@ def test_trace_level(log: ModmailLogger) -> None: assert "TRACE" in resp assert trace_test_phrase in resp + assert False # noqa: B011