@@ -114,112 +114,6 @@ def pytest_runtest_protocol(
114114 else :
115115 yield
116116
117- @pytest .hookimpl (hookwrapper = True )
118- def pytest_runtest_teardown (
119- self , item : _pytest .nodes .Item
120- ) -> typing .Generator [None , None , None ]:
121- if self .tracer :
122- # Since there is no pytest_fixture_teardown hook, we have to be a
123- # little clever to capture the spans for each fixture's teardown.
124- # The pytest_fixture_post_finalizer hook is called at the end of a
125- # fixture's teardown, but we don't know when the fixture actually
126- # began tearing down.
127- #
128- # Instead start a span here for the first fixture to be torn down,
129- # but give it a temporary name, since we don't know which fixture it
130- # will be. Then, in pytest_fixture_post_finalizer, when we do know
131- # which fixture is being torn down, update the name and attributes
132- # to the actual fixture, end the span, and create the span for the
133- # next fixture in line to be torn down.
134- self ._fixture_teardown_span = self .tracer .start_span ("fixture teardown" )
135- yield
136- # The last call to pytest_fixture_post_finalizer will create
137- # a span that is unneeded, so delete it.
138- del self ._fixture_teardown_span
139- else :
140- yield
141-
142- def _attributes_from_fixturedef (
143- self , fixturedef : _pytest .fixtures .FixtureDef [typing .Any ]
144- ) -> dict [str , str | int ]:
145- return {
146- SpanAttributes .CODE_FILEPATH : fixturedef .func .__code__ .co_filename ,
147- SpanAttributes .CODE_FUNCTION : fixturedef .argname ,
148- SpanAttributes .CODE_LINENO : fixturedef .func .__code__ .co_firstlineno ,
149- "test.fixture.scope" : fixturedef .scope ,
150- "test.scope" : "fixture" ,
151- }
152-
153- def _name_from_fixturedef (
154- self ,
155- fixturedef : _pytest .fixtures .FixtureDef [typing .Any ],
156- request : _pytest .fixtures .FixtureRequest ,
157- ) -> str :
158- if fixturedef .params and "request" in fixturedef .argnames :
159- try :
160- parameter = str (request .param )
161- except Exception :
162- parameter = str (
163- request .param_index
164- if isinstance (request , _pytest .fixtures .SubRequest )
165- else "?"
166- )
167- return f"{ fixturedef .argname } [{ parameter } ]"
168- return fixturedef .argname
169-
170- @pytest .hookimpl (hookwrapper = True )
171- def pytest_fixture_setup (
172- self ,
173- fixturedef : _pytest .fixtures .FixtureDef [typing .Any ],
174- request : _pytest .fixtures .FixtureRequest ,
175- ) -> typing .Generator [None , None , None ]:
176- if self .tracer :
177- with self .tracer .start_as_current_span (
178- name = f"{ self ._name_from_fixturedef (fixturedef , request )} setup" ,
179- attributes = self ._attributes_from_fixturedef (fixturedef ),
180- ):
181- yield
182- else :
183- yield
184-
185- @pytest .hookimpl (hookwrapper = True )
186- def pytest_fixture_post_finalizer (
187- self ,
188- fixturedef : _pytest .fixtures .FixtureDef [typing .Any ],
189- request : _pytest .fixtures .SubRequest ,
190- ) -> typing .Generator [None , None , None ]:
191- """When the span for a fixture teardown is created by
192- pytest_runtest_teardown or a previous pytest_fixture_post_finalizer, we
193- need to update the name and attributes now that we know which fixture it
194- was for."""
195-
196- if self .tracer :
197- # If the fixture has already been torn down, then it will have no cached
198- # result, so we can skip this one.
199- if fixturedef .cached_result is None :
200- yield
201- # Passing `-x` option to pytest can cause it to exit early so it may not
202- # have this span attribute.
203- elif not hasattr (self , "_fixture_teardown_span" ): # pragma: no cover
204- yield
205- else :
206- # If we've gotten here, we have a real fixture about to be torn down.
207- name = f"{ self ._name_from_fixturedef (fixturedef , request )} teardown"
208- self ._fixture_teardown_span .update_name (name )
209- attributes = self ._attributes_from_fixturedef (fixturedef )
210- self ._fixture_teardown_span .set_attributes (
211- attributes # type: ignore[arg-type]
212- )
213- yield
214- self ._fixture_teardown_span .end ()
215-
216- # Create the span for the next fixture to be torn down. When there are
217- # no more fixtures remaining, this will be an empty, useless span, so it
218- # needs to be deleted by pytest_runtest_teardown.
219- self ._fixture_teardown_span = self .tracer .start_span ("fixture teardown" )
220- else :
221- yield
222-
223117 def pytest_exception_interact (
224118 self ,
225119 node : _pytest .nodes .Node ,
0 commit comments