From 08afbb566efc44ffe04a45ade67213081fa0c42b Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 23 Jan 2025 10:01:40 +0100 Subject: [PATCH] feat: add test.case.result.status Fixes MRGFY-4513 Change-Id: I42f9ce532b29c5219543df822e9b630c2e72d1c2 --- pytest_mergify/__init__.py | 9 ++++++++- tests/test_spans.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pytest_mergify/__init__.py b/pytest_mergify/__init__.py index 486fce3..3a39d99 100644 --- a/pytest_mergify/__init__.py +++ b/pytest_mergify/__init__.py @@ -271,7 +271,14 @@ def pytest_runtest_logreport(self, report: _pytest.reports.TestReport) -> None: else opentelemetry.trace.StatusCode.OK ) self.has_error |= has_error - opentelemetry.trace.get_current_span().set_status(status_code) + + test_span = opentelemetry.trace.get_current_span() + test_span.set_status(status_code) + test_span.set_attributes( + { + "test.case.result.status": report.outcome, + } + ) def pytest_addoption(parser: _pytest.config.argparsing.Parser) -> None: diff --git a/tests/test_spans.py b/tests/test_spans.py index 0113822..b190c3d 100644 --- a/tests/test_spans.py +++ b/tests/test_spans.py @@ -48,6 +48,7 @@ def test_test( "code.lineno": 0, "code.filepath": "test_test.py", "test.case.name": "test_test.py::test_pass", + "test.case.result.status": "passed", } assert ( spans["test_test.py::test_pass"].status.status_code @@ -67,6 +68,7 @@ def test_test_failure( session_span = spans["pytest session start"] assert spans["test_test_failure.py::test_error"].attributes == { + "test.case.result.status": "failed", "test.type": "case", "code.function": "test_error", "code.lineno": 0, @@ -96,6 +98,36 @@ def test_test_failure( ) +def test_test_skipped( + pytester_with_spans: conftest.PytesterWithSpanT, +) -> None: + result, spans = pytester_with_spans(""" +import pytest +def test_skipped(): + pytest.skip('not needed') +""") + session_span = spans["pytest session start"] + + assert spans["test_test_skipped.py::test_skipped"].attributes == { + "test.case.result.status": "skipped", + "test.type": "case", + "code.function": "test_skipped", + "code.lineno": 1, + "code.filepath": "test_test_skipped.py", + "test.case.name": "test_test_skipped.py::test_skipped", + } + assert ( + spans["test_test_skipped.py::test_skipped"].status.status_code + == opentelemetry.trace.StatusCode.OK + ) + assert session_span.context is not None + assert spans["test_test_skipped.py::test_skipped"].parent is not None + assert ( + spans["test_test_skipped.py::test_skipped"].parent.span_id + == session_span.context.span_id + ) + + def test_fixture( pytester_with_spans: conftest.PytesterWithSpanT, ) -> None: