Skip to content

Commit 4aa34f6

Browse files
chore(ci_visibility): ensure exception dictionary is cleared after each test (#14344)
The solution in PR #14328 was incomplete. This PR ensures all exceptions are cleared up at the end of each test. I'm not adding a release note because this is a continuation of #14328, which already has a release note. - [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)) - [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) (cherry picked from commit a5aee01)
1 parent 069e5c9 commit 4aa34f6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

ddtrace/contrib/internal/pytest/_plugin_v2.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,16 +485,21 @@ def _pytest_runtest_protocol_post_yield(item, nextitem, coverage_collector):
485485
suite_id = test_id.parent_id
486486
module_id = suite_id.parent_id
487487

488+
reports_dict = reports_by_item.pop(item, None)
489+
488490
if not InternalTest.is_finished(test_id):
489491
log.debug("Test %s was not finished normally during pytest_runtest_protocol, finishing it now", test_id)
490-
reports_dict = reports_by_item.pop(item, None)
491492
if reports_dict:
492493
test_outcome = _process_reports_dict(item, reports_dict)
493494
InternalTest.finish(test_id, test_outcome.status, test_outcome.skip_reason, test_outcome.exc_info)
494495
else:
495496
log.debug("Test %s has no entry in reports_by_item", test_id)
496497
InternalTest.finish(test_id)
497498

499+
if reports_dict:
500+
for report in reports_dict.values():
501+
excinfo_by_report.pop(report, None)
502+
498503
if coverage_collector is not None:
499504
_handle_collected_coverage(test_id, coverage_collector)
500505

tests/contrib/pytest/test_pytest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import ddtrace
1010
from ddtrace.constants import _SAMPLING_PRIORITY_KEY
1111
from ddtrace.constants import ERROR_MSG
12+
from ddtrace.contrib.internal.pytest._utils import excinfo_by_report
13+
from ddtrace.contrib.internal.pytest._utils import reports_by_item
1214
from ddtrace.contrib.internal.pytest.constants import XFAIL_REASON
1315
from ddtrace.contrib.internal.pytest.patch import get_version
1416
from ddtrace.contrib.internal.pytest.plugin import is_enabled
@@ -4275,6 +4277,21 @@ def test_log_capture():
42754277
assert "I/O operation on closed file" not in result.stderr.str()
42764278
assert result.ret == 0
42774279

4280+
def test_pytest_clears_excinfo_dict_after_use(self):
4281+
reports_by_item_count_before = len(reports_by_item)
4282+
excinfo_by_report_count_before = len(excinfo_by_report)
4283+
4284+
self.testdir.makepyfile(
4285+
"""
4286+
def test_one():
4287+
assert False
4288+
"""
4289+
)
4290+
4291+
self.inline_run("--ddtrace")
4292+
assert len(reports_by_item) == reports_by_item_count_before
4293+
assert len(excinfo_by_report) == excinfo_by_report_count_before
4294+
42784295

42794296
def test_pytest_coverage_data_format_handling_none_value():
42804297
"""Test that coverage data format issues are handled correctly with proper logging for None value."""

0 commit comments

Comments
 (0)