Skip to content

Commit

Permalink
feat: add test.case.result.status
Browse files Browse the repository at this point in the history
Fixes MRGFY-4513

Change-Id: I42f9ce532b29c5219543df822e9b630c2e72d1c2
  • Loading branch information
jd committed Jan 23, 2025
1 parent c9058b9 commit 08afbb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pytest_mergify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_spans.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 08afbb5

Please sign in to comment.