Skip to content

Commit a45b659

Browse files
fix(ci_visibility): only enable coverage if test skipping is enabled (#7564)
Prevents enabling coverage (and using the `coverage` module) when using the unittest integration if the Intelligent Test Runner is not enabled. The `coverage` module is not a required dependency for CI Visibility (only for ITR) and shouldn't be assumed to be available and installed. ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [x] Documentation is included (in-code, generated user docs, [public corp docs](https://github.com/DataDog/documentation/)). - [x] Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. - [x] 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) - [x] If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from `@DataDog/security-design-and-guidance`. - [x] This PR doesn't touch any of that.
1 parent eb220c9 commit a45b659

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

ddtrace/contrib/unittest/patch.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,9 @@ def handle_test_wrapper(func, instance, args: tuple, kwargs: dict):
601601
)
602602
result.stopTest(test=instance)
603603
else:
604-
coverage = _start_coverage(root_directory)
605-
instance._coverage = coverage
604+
if _CIVisibility._instance._collect_coverage_enabled:
605+
coverage = _start_coverage(root_directory)
606+
instance._coverage = coverage
606607
result = func(*args, **kwargs)
607608
_update_status_item(test_suite_span, span.get_tag(test.STATUS))
608609
if hasattr(instance, "_coverage"):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
CI Visibility: fixes a crash where the unittest integration would try to enable coverage when tests are run even
5+
if the Intelligent Test Runner is not enabled.

tests/snapshots/tests.contrib.unittest_plugin.test_unittest_snapshot.test_unittest_generates_source_file_data.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@
131131
"span.kind": "test",
132132
"test.class_hierarchy": "CoverageTestCase",
133133
"test.command": "python -m unittest",
134-
"test.coverage": "{\"files\": [{\"filename\": \"test_source_file.py\", \"segments\": [[5, 0, 6, 0, -1]]}, {\"filename\": \"lib_fn.py\", \"segments\": [[1, 0, 2, 0, -1]]}]}",
135134
"test.framework": "unittest",
136135
"test.framework_version": "3.10.11",
137136
"test.module": "test_source_file",
@@ -174,7 +173,6 @@
174173
"span.kind": "test",
175174
"test.class_hierarchy": "CoverageTestCase",
176175
"test.command": "python -m unittest",
177-
"test.coverage": "{\"files\": [{\"filename\": \"test_source_file.py\", \"segments\": [[8, 0, 9, 0, -1]]}, {\"filename\": \"ret_false.py\", \"segments\": [[1, 0, 2, 0, -1]]}]}",
178176
"test.framework": "unittest",
179177
"test.framework_version": "3.10.11",
180178
"test.module": "test_source_file",
@@ -221,7 +219,6 @@
221219
"span.kind": "test",
222220
"test.class_hierarchy": "CoverageTestCase",
223221
"test.command": "python -m unittest",
224-
"test.coverage": "{\"files\": [{\"filename\": \"test_source_file.py\", \"segments\": [[11, 0, 12, 0, -1]]}, {\"filename\": \"ret_false.py\", \"segments\": [[2, 0, 2, 0, -1]]}]}",
225222
"test.framework": "unittest",
226223
"test.framework_version": "3.10.11",
227224
"test.module": "test_source_file",

0 commit comments

Comments
 (0)