From bdca2f3a297877e7b7c1d90b8052a62145839b68 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 24 Nov 2021 18:53:05 -0500 Subject: [PATCH 1/2] tests: add annotations for test failures add annotations with https://docs.github.com/en/actions/learn-github-actions/workflow-commands-for-github-actions --- tests/conftest.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index 5871ed8e..8af83fc3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,44 @@ +import copy +import os +import sys +from collections import OrderedDict +from typing import TYPE_CHECKING + 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 + ) + ) From 0e1f3f32d29fb0137d2544d98aa1f9b7766dbdf4 Mon Sep 17 00:00:00 2001 From: onerandomusername Date: Wed, 24 Nov 2021 18:57:10 -0500 Subject: [PATCH 2/2] tests: add xfail for annotation example --- tests/modmail/test_logs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/modmail/test_logs.py b/tests/modmail/test_logs.py index 97fffaf1..ad0882c7 100644 --- a/tests/modmail/test_logs.py +++ b/tests/modmail/test_logs.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" @@ -56,3 +57,4 @@ def test_trace_level(log: ModmailLogger) -> None: assert "TRACE" in resp assert trace_test_phrase in resp + assert False # noqa: B011