-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into brettlangdon/improve.…
…assertion
- Loading branch information
Showing
17 changed files
with
280 additions
and
84 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,110 @@ | ||
"""Provides functionality to support the pytest-bdd plugin as part of the ddtrace integration | ||
NOTE: This replaces the previous ddtrace.pytest_bdd plugin. | ||
This plugin mainly modifies the names of the test, its suite, and parameters. It does not, however modify the tests' | ||
suite from the perspective of Test Visibility data. | ||
The plugin is only instantiated and added if the pytest-bdd plugin itself is installed and enabled, because the hook | ||
implementations will cause errors unless the hookspecs are added by the original plugin. | ||
""" | ||
from pathlib import Path | ||
import sys | ||
|
||
import pytest | ||
|
||
from ddtrace.contrib.pytest._utils import _get_test_id_from_item | ||
from ddtrace.contrib.pytest_bdd import get_version | ||
from ddtrace.contrib.pytest_bdd._plugin import _extract_span | ||
from ddtrace.contrib.pytest_bdd._plugin import _get_step_func_args_json | ||
from ddtrace.contrib.pytest_bdd._plugin import _store_span | ||
from ddtrace.contrib.pytest_bdd.constants import FRAMEWORK | ||
from ddtrace.contrib.pytest_bdd.constants import STEP_KIND | ||
from ddtrace.ext import test | ||
from ddtrace.internal.logger import get_logger | ||
from ddtrace.internal.test_visibility.api import InternalTest | ||
from ddtrace.internal.test_visibility.api import InternalTestSession | ||
|
||
|
||
log = get_logger(__name__) | ||
|
||
|
||
def _get_workspace_relative_path(feature_path_str: str) -> Path: | ||
feature_path = Path(feature_path_str).resolve() | ||
workspace_path = InternalTestSession.get_workspace_path() | ||
if workspace_path: | ||
try: | ||
return feature_path.relative_to(workspace_path) | ||
except ValueError: # noqa: E722 | ||
log.debug("Feature path %s is not relative to workspace path %s", feature_path, workspace_path) | ||
return feature_path | ||
|
||
|
||
class _PytestBddSubPlugin: | ||
def __init__(self): | ||
self.framework_version = get_version() | ||
|
||
@staticmethod | ||
@pytest.hookimpl(tryfirst=True) | ||
def pytest_bdd_before_scenario(request, feature, scenario): | ||
test_id = _get_test_id_from_item(request.node) | ||
feature_path = _get_workspace_relative_path(scenario.feature.filename) | ||
codeowners = InternalTestSession.get_path_codeowners(feature_path) | ||
|
||
InternalTest.overwrite_attributes( | ||
test_id, name=scenario.name, suite_name=str(feature_path), codeowners=codeowners | ||
) | ||
|
||
@pytest.hookimpl(tryfirst=True) | ||
def pytest_bdd_before_step(self, request, feature, scenario, step, step_func): | ||
feature_test_id = _get_test_id_from_item(request.node) | ||
|
||
feature_span = InternalTest.get_span(feature_test_id) | ||
|
||
tracer = InternalTestSession.get_tracer() | ||
if tracer is None: | ||
return | ||
|
||
span = tracer.start_span( | ||
step.type, | ||
resource=step.name, | ||
span_type=STEP_KIND, | ||
child_of=feature_span, | ||
activate=True, | ||
) | ||
span.set_tag_str("component", "pytest_bdd") | ||
|
||
span.set_tag(test.FRAMEWORK, FRAMEWORK) | ||
span.set_tag(test.FRAMEWORK_VERSION, self.framework_version) | ||
|
||
feature_path = _get_workspace_relative_path(scenario.feature.filename) | ||
|
||
span.set_tag(test.FILE, str(feature_path)) | ||
span.set_tag(test.CODEOWNERS, InternalTestSession.get_path_codeowners(feature_path)) | ||
|
||
_store_span(step_func, span) | ||
|
||
@staticmethod | ||
@pytest.hookimpl(trylast=True) | ||
def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args): | ||
span = _extract_span(step_func) | ||
if span is not None: | ||
step_func_args_json = _get_step_func_args_json(step, step_func, step_func_args) | ||
if step_func_args: | ||
span.set_tag(test.PARAMETERS, step_func_args_json) | ||
span.finish() | ||
|
||
@staticmethod | ||
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception): | ||
span = _extract_span(step_func) | ||
if span is not None: | ||
if hasattr(exception, "__traceback__"): | ||
tb = exception.__traceback__ | ||
else: | ||
# PY2 compatibility workaround | ||
_, _, tb = sys.exc_info() | ||
step_func_args_json = _get_step_func_args_json(step, step_func, step_func_args) | ||
if step_func_args: | ||
span.set_tag(test.PARAMETERS, step_func_args_json) | ||
span.set_exc_info(type(exception), exception, tb) | ||
span.finish() |
This file contains 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 |
---|---|---|
@@ -1,9 +1,20 @@ | ||
from ddtrace import DDTraceDeprecationWarning | ||
from ddtrace.contrib.pytest._utils import _USE_PLUGIN_V2 | ||
from ddtrace.contrib.pytest.plugin import is_enabled as is_ddtrace_enabled | ||
from ddtrace.vendor.debtcollector import deprecate | ||
|
||
|
||
def pytest_configure(config): | ||
if config.pluginmanager.hasplugin("pytest-bdd") and config.pluginmanager.hasplugin("ddtrace"): | ||
if is_ddtrace_enabled(config): | ||
from ._plugin import _PytestBddPlugin | ||
if not _USE_PLUGIN_V2: | ||
if is_ddtrace_enabled(config): | ||
from ._plugin import _PytestBddPlugin | ||
|
||
config.pluginmanager.register(_PytestBddPlugin(), "_datadog-pytest-bdd") | ||
deprecate( | ||
"the ddtrace.pytest_bdd plugin is deprecated", | ||
message="it will be integrated with the main pytest ddtrace plugin", | ||
removal_version="3.0.0", | ||
category=DDTraceDeprecationWarning, | ||
) | ||
|
||
config.pluginmanager.register(_PytestBddPlugin(), "_datadog-pytest-bdd") |
This file contains 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 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.