Skip to content

Commit edb6a11

Browse files
chore(ci_visibility): ensure exception dictionary is cleared after each test [backport 3.12] (#14346)
Backport a5aee01 from #14344 to 3.12. 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. ## 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) Co-authored-by: Vítor De Araújo <vitor.dearaujo@datadoghq.com>
1 parent b192a5b commit edb6a11

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
@@ -581,16 +581,21 @@ def _pytest_runtest_protocol_post_yield(item, nextitem, coverage_collector):
581581
if coverage_collector is not None and not InternalTest.is_finished(test_id):
582582
_handle_collected_coverage(item, test_id, coverage_collector)
583583

584+
reports_dict = reports_by_item.pop(item, None)
585+
584586
if not InternalTest.is_finished(test_id):
585587
log.debug("Test %s was not finished normally during pytest_runtest_protocol, finishing it now", test_id)
586-
reports_dict = reports_by_item.pop(item, None)
587588
if reports_dict:
588589
test_outcome = _process_reports_dict(item, reports_dict)
589590
InternalTest.finish(test_id, test_outcome.status, test_outcome.skip_reason, test_outcome.exc_info)
590591
else:
591592
log.debug("Test %s has no entry in reports_by_item", test_id)
592593
InternalTest.finish(test_id)
593594

595+
if reports_dict:
596+
for report in reports_dict.values():
597+
excinfo_by_report.pop(report, None)
598+
594599
# We rely on the CI Visibility service to prevent finishing items that have been discovered and have unfinished
595600
# children, but as an optimization:
596601
# - we know we don't need to finish the suite if the next item is in the same suite

tests/contrib/pytest/test_pytest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from ddtrace.contrib.internal.pytest._utils import _pytest_version_supports_atr
1313
from ddtrace.contrib.internal.pytest._utils import _pytest_version_supports_efd
1414
from ddtrace.contrib.internal.pytest._utils import _pytest_version_supports_itr
15+
from ddtrace.contrib.internal.pytest._utils import excinfo_by_report
16+
from ddtrace.contrib.internal.pytest._utils import reports_by_item
1517
from ddtrace.contrib.internal.pytest.constants import XFAIL_REASON
1618
from ddtrace.contrib.internal.pytest.patch import get_version
1719
from ddtrace.contrib.internal.pytest.plugin import is_enabled
@@ -4591,6 +4593,21 @@ def test_log_capture():
45914593
assert "I/O operation on closed file" not in result.stderr.str()
45924594
assert result.ret == 0
45934595

4596+
def test_pytest_clears_excinfo_dict_after_use(self):
4597+
reports_by_item_count_before = len(reports_by_item)
4598+
excinfo_by_report_count_before = len(excinfo_by_report)
4599+
4600+
self.testdir.makepyfile(
4601+
"""
4602+
def test_one():
4603+
assert False
4604+
"""
4605+
)
4606+
4607+
self.inline_run("--ddtrace")
4608+
assert len(reports_by_item) == reports_by_item_count_before
4609+
assert len(excinfo_by_report) == excinfo_by_report_count_before
4610+
45944611
@pytest.mark.skipif(
45954612
not _PYTEST_SUPPORTS_ITR or not _PYTEST_SUPPORTS_EFD,
45964613
reason=f"pytest version {get_version()} does not support EFD or ITR coverage reporting",

0 commit comments

Comments
 (0)