Skip to content

Commit

Permalink
feat: add test framework tags (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
jd authored Dec 20, 2024
1 parent fd8cdb0 commit 660e1a7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pytest_mergify/resources/pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from opentelemetry.sdk.resources import Resource, ResourceDetector


class PytestResourceDetector(ResourceDetector):
"""Detects OpenTelemetry Resource attributes for Pytest."""

def detect(self) -> Resource:
return Resource(
{
"test.framework": "pytest",
"test.framework.version": pytest.__version__,
}
)
2 changes: 2 additions & 0 deletions pytest_mergify/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pytest_opentelemetry.instrumentation
import pytest_mergify.resources.ci as resources_ci
import pytest_mergify.resources.github_actions as resources_gha
import pytest_mergify.resources.pytest as resources_pytest


class InterceptingSpanProcessor(SpanProcessor):
Expand Down Expand Up @@ -100,6 +101,7 @@ def __post_init__(self) -> None:
[
resources_ci.CIResourceDetector(),
resources_gha.GitHubActionsResourceDetector(),
resources_pytest.PytestResourceDetector(),
]
)

Expand Down
26 changes: 25 additions & 1 deletion tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from tests import conftest


def test_span_resources_attributes(
def test_span_resources_attributes_ci(
pytester: _pytest.pytester.Pytester,
reconfigure_mergify_tracer: conftest.ReconfigureT,
) -> None:
Expand All @@ -28,6 +28,30 @@ def test_span(pytestconfig):
result.assert_outcomes(passed=1)


def test_span_resources_attributes_pytest(
pytester: _pytest.pytester.Pytester,
reconfigure_mergify_tracer: conftest.ReconfigureT,
) -> None:
reconfigure_mergify_tracer({"_PYTEST_MERGIFY_TEST": "true"})
pytester.makepyfile(
"""
import re
import pytest
def test_span(pytestconfig):
plugin = pytestconfig.pluginmanager.get_plugin("PytestMergify")
assert plugin is not None
assert plugin.mergify_tracer.exporter is not None
spans = plugin.mergify_tracer.exporter.get_finished_spans()
assert spans[0].resource.attributes["test.framework"] == "pytest"
assert re.match(r"\d\.", spans[0].resource.attributes["test.framework.version"])
"""
)
result = pytester.runpytest_subprocess()
result.assert_outcomes(passed=1)


def test_span_github_actions(
pytester: _pytest.pytester.Pytester,
reconfigure_mergify_tracer: conftest.ReconfigureT,
Expand Down

0 comments on commit 660e1a7

Please sign in to comment.