Skip to content

tests: add annotations for test failures #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down
2 changes: 2 additions & 0 deletions tests/modmail/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
log: ModmailLogger = logging.getLogger(__name__)
return log


Check failure on line 32 in tests/modmail/test_log.py

View workflow job for this annotation

GitHub Actions / test (3.8, ubuntu-latest)

test_notice_level

Unexpected test success.

Check failure on line 32 in tests/modmail/test_log.py

View workflow job for this annotation

GitHub Actions / test (3.9, ubuntu-latest)

test_notice_level

Unexpected test success.
@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"
Expand Down Expand Up @@ -59,3 +60,4 @@

assert "TRACE" in resp
assert trace_test_phrase in resp
assert False # noqa: B011
Loading