Skip to content

Commit 5585f26

Browse files
authored
tests: use upstream pytest-bdd traces (#1027)
* tests: use upstream pytest-bdd traces * add spans for undo
1 parent 4cf31e3 commit 5585f26

File tree

3 files changed

+33
-53
lines changed

3 files changed

+33
-53
lines changed

.github/workflows/test_integration.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ jobs:
5959
python -m pip install --upgrade pip
6060
pip install --upgrade wheel setuptools build
6161
- name: Install
62-
run: |
63-
pip install --disable-pip-version-check git+https://github.com/DataDog/dd-trace-py.git#egg=ddtrace
64-
pip install --disable-pip-version-check -e .[tests]
62+
run: pip install --disable-pip-version-check -e .[apm,tests]
6563
- name: Test
6664
run: ./run-tests.sh
6765
shell: bash

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ setup_requires =
4949

5050
[options.extras_require]
5151
apm =
52-
ddtrace>=1.0.0rc1
52+
ddtrace>=1.2.0
5353
async =
5454
aiosonic
5555
zstandard =

tests/conftest.py

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
4242
"%40test.service%3A{}%20%40ci.pipeline.id%3A{}&index=citest".format(dd_service, ci_pipeline_id)
4343
)
4444

45+
4546
except ImportError:
4647
if os.getenv("CI", "false") == "true" and RECORD == "none":
4748
raise
@@ -104,24 +105,6 @@ def escape_reserved_keyword(word):
104105
return word
105106

106107

107-
def pytest_bdd_before_scenario(request, feature, scenario):
108-
if tracer is not None:
109-
span = tracer.current_span()
110-
if span is not None:
111-
span.set_tag("test.name", scenario.name)
112-
span.set_tag("test.suite", scenario.feature.filename.split("tests")[-1])
113-
114-
codeowners = [f"@{tag[5:]}" for tag in scenario.tags | scenario.feature.tags if tag.startswith("team:")]
115-
if codeowners:
116-
try:
117-
default_value = span.get_tag("test.codeowners")
118-
default_codeowners = json.loads(default_value)
119-
codeowners.extend(default_codeowners)
120-
except Exception:
121-
pass
122-
span.set_tag("test.codeowners", json.dumps(codeowners))
123-
124-
125108
def pytest_bdd_after_scenario(request, feature, scenario):
126109
try:
127110
ctx = request.getfixturevalue("context")
@@ -131,33 +114,6 @@ def pytest_bdd_after_scenario(request, feature, scenario):
131114
undo()
132115

133116

134-
def pytest_bdd_before_step(request, feature, scenario, step, step_func):
135-
if tracer is None:
136-
return
137-
138-
span = tracer.start_span(
139-
step.type,
140-
resource=step.name,
141-
span_type=step.type,
142-
child_of=tracer.current_span(),
143-
activate=True,
144-
)
145-
setattr(step_func, "__dd_span__", span)
146-
147-
148-
def pytest_bdd_after_step(request, feature, scenario, step, step_func, step_func_args):
149-
span = getattr(step_func, "__dd_span__", None)
150-
if span is not None:
151-
span.finish()
152-
153-
154-
def pytest_bdd_step_error(request, feature, scenario, step, step_func, step_func_args, exception):
155-
span = getattr(step_func, "__dd_span__", None)
156-
if span is not None:
157-
span.set_exc_info(type(exception), exception, exception.__traceback__)
158-
span.finish()
159-
160-
161117
def pytest_bdd_apply_tag(tag, function):
162118
"""Register tags as custom markers and skip test for '@skip' ones."""
163119
skip_tags = {"skip", "skip-python"}
@@ -283,8 +239,22 @@ def disable_recording(request):
283239
@pytest.fixture
284240
def vcr_config():
285241
config = dict(
286-
filter_headers=("DD-API-KEY", "DD-APPLICATION-KEY", "User-Agent", "Accept-Encoding"),
287-
match_on=["method", "scheme", "host", "port", "path", "query", "body", "headers"],
242+
filter_headers=(
243+
"DD-API-KEY",
244+
"DD-APPLICATION-KEY",
245+
"User-Agent",
246+
"Accept-Encoding",
247+
),
248+
match_on=[
249+
"method",
250+
"scheme",
251+
"host",
252+
"port",
253+
"path",
254+
"query",
255+
"body",
256+
"headers",
257+
],
288258
)
289259
if tracer:
290260
from urllib.parse import urlparse
@@ -501,7 +471,13 @@ def build_param(p):
501471
result = operation_method(**kwargs)
502472

503473
# register undo method
504-
context["undo_operations"].append(lambda: undo(api, version, operation_name, result, client=client))
474+
def undo_operation():
475+
return undo(api, version, operation_name, result, client=client)
476+
477+
if tracer:
478+
undo_operation = tracer.wrap(name="undo", resource=operation["step"])(undo_operation)
479+
480+
context["undo_operations"].append(undo_operation)
505481

506482
# optional re-shaping
507483
if "source" in operation:
@@ -580,7 +556,13 @@ def execute_request(undo, context, client, api_version):
580556
operation_id = api_request["request"].__name__
581557
response = api_request["response"][0]
582558

583-
context["undo_operations"].append(lambda: undo(api, api_version, operation_id, response))
559+
def undo_operation():
560+
return undo(api, api_version, operation_id, response)
561+
562+
if tracer:
563+
undo_operation = tracer.wrap(name="undo", resource="execute request")(undo_operation)
564+
565+
context["undo_operations"].append(undo_operation)
584566

585567

586568
@when("the request with pagination is sent")

0 commit comments

Comments
 (0)