-
Notifications
You must be signed in to change notification settings - Fork 514
chore(testing): add test discovery mode for pytest #18559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 17 commits into
main
from
gnufede/ddtest-discovery-format
Jun 12, 2026
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4b59b15
feat(ci_visibility): add test discovery mode for pytest
gnufede 7f52620
fix(ci_visibility): use env.get() instead of os.environ.get in discov…
gnufede 221015a
fix(ci_visibility): adopt ddtest env var names for discovery mode
gnufede a4b13c3
fix(ci_visibility): use dotted directory path as module in discovery …
gnufede dfe9abd
refactor(ci_visibility): share item_to_test_ref code path in discover…
gnufede 910db5d
chore(testing): register DD_TEST_OPTIMIZATION_DISCOVERY_* env vars
gnufede cbd52f3
fix(testing): fix import order and discovery hook registration in pyt…
gnufede 70b4b0a
fix(testing): suppress mypy misc error on pytest.hookimpl decorator
gnufede 05c9977
fix(testing): use monkeypatch fixture instead of pytester.monkeypatch
gnufede 12ec7ca
Merge branch 'main' into gnufede/ddtest-discovery-format
gnufede c9050e9
fix(ci_visibility): treat bare skipif (no condition) as unconditional…
gnufede 2852eb1
revert(ci_visibility): restore original condition=None handling in _i…
gnufede 986172d
docs(ci_visibility): comment why condition=None/str are conservativel…
gnufede d47bea6
docs(ci_visibility): correct comment about bare skipif (no condition …
gnufede c12c640
chore(ci_visibility): address PR review comments on discovery mode
gnufede b2b56f3
chore(ci_visibility): fix import sort order in test_plugin.py
gnufede d76a31e
Merge branch 'main' into gnufede/ddtest-discovery-format
gnufede File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
| from pathlib import Path | ||
| import typing as t | ||
|
|
||
| import pytest | ||
|
|
||
| from ddtrace.internal.settings import env | ||
| from ddtrace.testing.internal.pytest.utils import _get_test_parameters_json | ||
| from ddtrace.testing.internal.pytest.utils import item_to_test_ref | ||
| from ddtrace.testing.internal.utils import asbool | ||
|
|
||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| _ENV_ENABLED = "DD_TEST_OPTIMIZATION_DISCOVERY_ENABLED" | ||
| _ENV_OUTPUT_PATH = "DD_TEST_OPTIMIZATION_DISCOVERY_FILE" | ||
| _DEFAULT_OUTPUT_PATH = ".testoptimization/tests-discovery/tests.json" | ||
|
|
||
|
|
||
| def is_discovery_mode_enabled() -> bool: | ||
| return asbool(env.get(_ENV_ENABLED)) | ||
|
|
||
|
|
||
| def _get_output_path() -> Path: | ||
| return Path(env.get(_ENV_OUTPUT_PATH, _DEFAULT_OUTPUT_PATH)) | ||
|
|
||
|
|
||
| def _is_item_skipped(item: pytest.Item) -> bool: | ||
| """Return True if the item will definitely be skipped at execution time. | ||
|
|
||
| Handles pytest.mark.skip (unconditional) and pytest.mark.skipif with | ||
| non-string conditions. String conditions are not evaluated (would require | ||
| exec in the test module's namespace) so those tests are conservatively | ||
| included. | ||
| """ | ||
| if item.get_closest_marker("skip") is not None: | ||
| return True | ||
| for marker in item.iter_markers("skipif"): | ||
| condition = marker.args[0] if marker.args else marker.kwargs.get("condition") | ||
| if condition is None or isinstance(condition, str): | ||
| # String conditions require eval in the test module's namespace, which | ||
| # we can't do safely at collection time. None conditions can't arise | ||
| # in practice (pytest >= 7 rejects bare skipif with no condition arg). | ||
| # Conservatively include in both cases rather than risk hiding a test. | ||
| continue | ||
|
gnufede marked this conversation as resolved.
|
||
| if condition: | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _get_suite_source_file(item: pytest.Item, workspace_path: t.Optional[Path]) -> str: | ||
| item_path = Path(item.path if hasattr(item, "path") else getattr(item, "fspath", "")).absolute() | ||
| if workspace_path is not None: | ||
| try: | ||
| # TODO: use item_path.relative_to(workspace_path).as_posix() on Windows | ||
|
gnufede marked this conversation as resolved.
Outdated
|
||
| return str(item_path.relative_to(workspace_path)) | ||
| except ValueError: | ||
| pass | ||
| return str(item_path) | ||
|
|
||
|
|
||
| @pytest.hookimpl(tryfirst=True) # type: ignore[misc] | ||
| def pytest_collection_finish(session: pytest.Session) -> None: | ||
| if not is_discovery_mode_enabled(): | ||
| return | ||
|
|
||
| workspace_path: t.Optional[Path] = None | ||
| try: | ||
| from ddtrace.testing.internal.git import get_workspace_path | ||
|
gnufede marked this conversation as resolved.
Outdated
|
||
|
|
||
| workspace_path = get_workspace_path() | ||
| except Exception: | ||
| log.debug("Could not determine workspace path for test discovery", exc_info=True) | ||
|
|
||
| output_path = _get_output_path() | ||
| output_path.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| with open(output_path, "w") as f: | ||
|
gnufede marked this conversation as resolved.
Outdated
|
||
| for item in session.items: | ||
| if _is_item_skipped(item): | ||
| continue | ||
|
|
||
| test_ref = item_to_test_ref(item) | ||
| module = test_ref.suite.module.name | ||
| suite = test_ref.suite.name | ||
| name = test_ref.name | ||
| parameters = _get_test_parameters_json(item) | ||
| suite_source_file = _get_suite_source_file(item, workspace_path) | ||
|
|
||
| test_info: dict[str, t.Any] = { | ||
| "name": name, | ||
| "suite": suite, | ||
| "module": module, | ||
| "parameters": parameters, | ||
| "suiteSourceFile": suite_source_file, | ||
| } | ||
| f.write(json.dumps(test_info) + "\n") | ||
|
|
||
| log.info("Test discovery complete: wrote tests to %s", output_path) | ||
| pytest.exit("Test discovery complete", returncode=0) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.