|
| 1 | +from unittest import mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ddtrace.contrib.internal.pytest._utils import _pytest_version_supports_itr |
| 6 | +from ddtrace.internal.ci_visibility._api_client import TestVisibilityAPISettings |
| 7 | +from ddtrace.internal.ci_visibility.constants import COVERAGE_TAG_NAME |
| 8 | +from tests.contrib.pytest.test_pytest import PytestTestCaseBase |
| 9 | +from tests.contrib.pytest.test_pytest import _get_spans_from_list |
| 10 | + |
| 11 | + |
| 12 | +_TEST_PASS_CONTENT = """ |
| 13 | +def test_func_pass(): |
| 14 | + assert True |
| 15 | +""" |
| 16 | + |
| 17 | +pytestmark = pytest.mark.skipif(not _pytest_version_supports_itr(), reason="pytest version does not support coverage") |
| 18 | + |
| 19 | + |
| 20 | +class PytestEarlyConfigTestCase(PytestTestCaseBase): |
| 21 | + """ |
| 22 | + Test that code coverage is enabled in the `pytest_load_initial_conftests` hook, regardless of the method used to |
| 23 | + enable ddtrace (command line, env var, or ini file). |
| 24 | + """ |
| 25 | + |
| 26 | + @pytest.fixture(autouse=True, scope="function") |
| 27 | + def set_up_features(self): |
| 28 | + with mock.patch( |
| 29 | + "ddtrace.internal.ci_visibility.recorder.CIVisibility._check_enabled_features", |
| 30 | + return_value=TestVisibilityAPISettings(coverage_enabled=True), |
| 31 | + ): |
| 32 | + yield |
| 33 | + |
| 34 | + def test_coverage_not_enabled(self): |
| 35 | + self.testdir.makepyfile(test_pass=_TEST_PASS_CONTENT) |
| 36 | + self.inline_run() |
| 37 | + spans = self.pop_spans() |
| 38 | + assert not spans |
| 39 | + |
| 40 | + def test_coverage_enabled_via_command_line_option(self): |
| 41 | + self.testdir.makepyfile(test_pass=_TEST_PASS_CONTENT) |
| 42 | + self.inline_run("--ddtrace") |
| 43 | + spans = self.pop_spans() |
| 44 | + [suite_span] = _get_spans_from_list(spans, "suite") |
| 45 | + [test_span] = _get_spans_from_list(spans, "test") |
| 46 | + assert ( |
| 47 | + suite_span.get_struct_tag(COVERAGE_TAG_NAME) is not None or test_span.get_tag(COVERAGE_TAG_NAME) is not None |
| 48 | + ) |
| 49 | + |
| 50 | + def test_coverage_enabled_via_pytest_addopts_env_var(self): |
| 51 | + self.testdir.makepyfile(test_pass=_TEST_PASS_CONTENT) |
| 52 | + self.inline_run(extra_env={"PYTEST_ADDOPTS": "--ddtrace"}) |
| 53 | + spans = self.pop_spans() |
| 54 | + [suite_span] = _get_spans_from_list(spans, "suite") |
| 55 | + [test_span] = _get_spans_from_list(spans, "test") |
| 56 | + assert ( |
| 57 | + suite_span.get_struct_tag(COVERAGE_TAG_NAME) is not None or test_span.get_tag(COVERAGE_TAG_NAME) is not None |
| 58 | + ) |
| 59 | + |
| 60 | + def test_coverage_enabled_via_addopts_ini_file_option(self): |
| 61 | + self.testdir.makepyfile(test_pass=_TEST_PASS_CONTENT) |
| 62 | + self.testdir.makefile(".ini", pytest="[pytest]\naddopts = --ddtrace\n") |
| 63 | + self.inline_run() |
| 64 | + spans = self.pop_spans() |
| 65 | + [suite_span] = _get_spans_from_list(spans, "suite") |
| 66 | + [test_span] = _get_spans_from_list(spans, "test") |
| 67 | + assert ( |
| 68 | + suite_span.get_struct_tag(COVERAGE_TAG_NAME) is not None or test_span.get_tag(COVERAGE_TAG_NAME) is not None |
| 69 | + ) |
| 70 | + |
| 71 | + def test_coverage_enabled_via_ddtrace_ini_file_option(self): |
| 72 | + self.testdir.makepyfile(test_pass=_TEST_PASS_CONTENT) |
| 73 | + self.testdir.makefile(".ini", pytest="[pytest]\nddtrace = 1\n") |
| 74 | + self.inline_run() |
| 75 | + spans = self.pop_spans() |
| 76 | + [suite_span] = _get_spans_from_list(spans, "suite") |
| 77 | + [test_span] = _get_spans_from_list(spans, "test") |
| 78 | + assert ( |
| 79 | + suite_span.get_struct_tag(COVERAGE_TAG_NAME) is not None or test_span.get_tag(COVERAGE_TAG_NAME) is not None |
| 80 | + ) |
0 commit comments