Skip to content

Commit 4d20f16

Browse files
fix(ci_visibility): ensure test span is finished even if pytest_runtest_protocol is overridden (#13614)
Currently, if the `pytest_runtest_protocol` hook is overridden in `conftest.py` and skips the Test Optimization hook, the corresponding test span will be left unfinished, which in turn will cause the suite and module to be unfinished until the end of the session. This PR ensures that the test will be finished by the `pytest_runtest_protocol` hook wrapper, so at least the other tests in the session are not affected. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent 995e706 commit 4d20f16

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

ddtrace/contrib/internal/pytest/_plugin_v2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ def _pytest_runtest_protocol_post_yield(item, nextitem, coverage_collector):
400400
suite_id = test_id.parent_id
401401
module_id = suite_id.parent_id
402402

403+
if not InternalTest.is_finished(test_id):
404+
log.debug("Test %s was not finished normally during pytest_runtest_protocol, finishing it now", test_id)
405+
InternalTest.finish(test_id)
406+
403407
if coverage_collector is not None:
404408
_handle_collected_coverage(test_id, coverage_collector)
405409

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
fixes:
3+
- |
4+
CI Visibility: This fix resolves an inssue where test spans would be left unfinished if the
5+
`pytest_runtest_protocol` hook was overridden in `conftest.py`, causing the corresponding module and suite to be
6+
unfinished as well.

0 commit comments

Comments
 (0)