Skip to content

Commit 883371e

Browse files
authored
chore: remove setup/call/teardown steps (#38)
This creates 3x more spans for each test which is too verbose.
1 parent 84b6fe3 commit 883371e

File tree

2 files changed

+14
-140
lines changed

2 files changed

+14
-140
lines changed

pytest_mergify/__init__.py

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -123,59 +123,25 @@ def pytest_runtest_protocol(
123123
else:
124124
yield
125125

126-
@pytest.hookimpl(hookwrapper=True)
127-
def pytest_runtest_setup(
128-
self, item: _pytest.nodes.Item
129-
) -> typing.Generator[None, None, None]:
130-
if self.tracer:
131-
with self.tracer.start_as_current_span(
132-
f"{item.nodeid}::setup",
133-
attributes=self._attributes_from_item(item)
134-
| {"test.type": "test.setup"},
135-
):
136-
yield
137-
else:
138-
yield
139-
140-
@pytest.hookimpl(hookwrapper=True)
141-
def pytest_runtest_call(
142-
self, item: _pytest.nodes.Item
143-
) -> typing.Generator[None, None, None]:
144-
if self.tracer:
145-
with self.tracer.start_as_current_span(
146-
name=f"{item.nodeid}::call",
147-
attributes=self._attributes_from_item(item)
148-
| {"test.type": "test.call"},
149-
):
150-
yield
151-
else:
152-
yield
153-
154126
@pytest.hookimpl(hookwrapper=True)
155127
def pytest_runtest_teardown(
156128
self, item: _pytest.nodes.Item
157129
) -> typing.Generator[None, None, None]:
158130
if self.tracer:
159-
with self.tracer.start_as_current_span(
160-
name=f"{item.nodeid}::teardown",
161-
attributes=self._attributes_from_item(item)
162-
| {"test.type": "test.teardown"},
163-
):
164-
# Since there is no pytest_fixture_teardown hook, we have to be a
165-
# little clever to capture the spans for each fixture's teardown.
166-
# The pytest_fixture_post_finalizer hook is called at the end of a
167-
# fixture's teardown, but we don't know when the fixture actually
168-
# began tearing down.
169-
#
170-
# Instead start a span here for the first fixture to be torn down,
171-
# but give it a temporary name, since we don't know which fixture it
172-
# will be. Then, in pytest_fixture_post_finalizer, when we do know
173-
# which fixture is being torn down, update the name and attributes
174-
# to the actual fixture, end the span, and create the span for the
175-
# next fixture in line to be torn down.
176-
self._fixture_teardown_span = self.tracer.start_span("fixture teardown")
177-
yield
178-
131+
# Since there is no pytest_fixture_teardown hook, we have to be a
132+
# little clever to capture the spans for each fixture's teardown.
133+
# The pytest_fixture_post_finalizer hook is called at the end of a
134+
# fixture's teardown, but we don't know when the fixture actually
135+
# began tearing down.
136+
#
137+
# Instead start a span here for the first fixture to be torn down,
138+
# but give it a temporary name, since we don't know which fixture it
139+
# will be. Then, in pytest_fixture_post_finalizer, when we do know
140+
# which fixture is being torn down, update the name and attributes
141+
# to the actual fixture, end the span, and create the span for the
142+
# next fixture in line to be torn down.
143+
self._fixture_teardown_span = self.tracer.start_span("fixture teardown")
144+
yield
179145
# The last call to pytest_fixture_post_finalizer will create
180146
# a span that is unneeded, so delete it.
181147
del self._fixture_teardown_span

tests/test_spans.py

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ def test_span(
1515
assert set(spans.keys()) == {
1616
"pytest session start",
1717
"test_span.py::test_pass",
18-
"test_span.py::test_pass::setup",
19-
"test_span.py::test_pass::call",
20-
"test_span.py::test_pass::teardown",
2118
}
2219

2320

@@ -45,15 +42,6 @@ def test_test(
4542
result, spans = pytester_with_spans()
4643
session_span = spans["pytest session start"]
4744

48-
expected_spans = (
49-
"test_test.py::test_pass",
50-
"test_test.py::test_pass::setup",
51-
"test_test.py::test_pass::call",
52-
"test_test.py::test_pass::teardown",
53-
)
54-
for name in expected_spans:
55-
assert name in spans
56-
5745
assert spans["test_test.py::test_pass"].attributes == {
5846
"test.type": "test",
5947
"code.function": "test_pass",
@@ -71,57 +59,13 @@ def test_test(
7159
spans["test_test.py::test_pass"].parent.span_id == session_span.context.span_id
7260
)
7361

74-
assert spans["test_test.py::test_pass::setup"].attributes == {
75-
"test.type": "test.setup",
76-
"code.function": "test_pass",
77-
"code.lineno": 0,
78-
"code.filepath": "test_test.py",
79-
"test.case.name": "test_test.py::test_pass",
80-
}
81-
assert (
82-
spans["test_test.py::test_pass::setup"].status.status_code
83-
== opentelemetry.trace.StatusCode.UNSET
84-
)
85-
86-
assert spans["test_test.py::test_pass::call"].attributes == {
87-
"test.type": "test.call",
88-
"code.function": "test_pass",
89-
"code.lineno": 0,
90-
"code.filepath": "test_test.py",
91-
"test.case.name": "test_test.py::test_pass",
92-
}
93-
assert (
94-
spans["test_test.py::test_pass::call"].status.status_code
95-
== opentelemetry.trace.StatusCode.UNSET
96-
)
97-
98-
assert spans["test_test.py::test_pass::teardown"].attributes == {
99-
"test.type": "test.teardown",
100-
"code.function": "test_pass",
101-
"code.lineno": 0,
102-
"code.filepath": "test_test.py",
103-
"test.case.name": "test_test.py::test_pass",
104-
}
105-
assert (
106-
spans["test_test.py::test_pass::teardown"].status.status_code
107-
== opentelemetry.trace.StatusCode.UNSET
108-
)
109-
11062

11163
def test_test_failure(
11264
pytester_with_spans: conftest.PytesterWithSpanT,
11365
) -> None:
11466
result, spans = pytester_with_spans("def test_error(): assert False, 'foobar'")
11567
session_span = spans["pytest session start"]
11668

117-
expected_spans = (
118-
"test_test_failure.py::test_error",
119-
"test_test_failure.py::test_error::setup",
120-
"test_test_failure.py::test_error::call",
121-
)
122-
for name in expected_spans:
123-
assert name in spans
124-
12569
assert spans["test_test_failure.py::test_error"].attributes == {
12670
"test.type": "test",
12771
"code.function": "test_error",
@@ -151,42 +95,6 @@ def test_test_failure(
15195
== session_span.context.span_id
15296
)
15397

154-
assert spans["test_test_failure.py::test_error::setup"].attributes == {
155-
"test.type": "test.setup",
156-
"code.function": "test_error",
157-
"code.lineno": 0,
158-
"code.filepath": "test_test_failure.py",
159-
"test.case.name": "test_test_failure.py::test_error",
160-
}
161-
assert (
162-
spans["test_test_failure.py::test_error::setup"].status.status_code
163-
== opentelemetry.trace.StatusCode.UNSET
164-
)
165-
166-
assert spans["test_test_failure.py::test_error::call"].attributes == {
167-
"test.type": "test.call",
168-
"code.function": "test_error",
169-
"code.lineno": 0,
170-
"code.filepath": "test_test_failure.py",
171-
"test.case.name": "test_test_failure.py::test_error",
172-
}
173-
assert (
174-
spans["test_test_failure.py::test_error::call"].status.status_code
175-
== opentelemetry.trace.StatusCode.UNSET
176-
)
177-
178-
assert spans["test_test_failure.py::test_error::teardown"].attributes == {
179-
"test.type": "test.teardown",
180-
"code.function": "test_error",
181-
"code.lineno": 0,
182-
"code.filepath": "test_test_failure.py",
183-
"test.case.name": "test_test_failure.py::test_error",
184-
}
185-
assert (
186-
spans["test_test_failure.py::test_error::teardown"].status.status_code
187-
== opentelemetry.trace.StatusCode.UNSET
188-
)
189-
19098

19199
def test_fixture(
192100
pytester_with_spans: conftest.PytesterWithSpanT,

0 commit comments

Comments
 (0)