Skip to content

Commit 3da2ee7

Browse files
committed
fix for execution
1 parent 85f2f8e commit 3da2ee7

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

pythonFiles/tests/pytestadapter/expected_execution_test_output.py

+13
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,16 @@
395395
"subtest": None,
396396
},
397397
}
398+
399+
# This is the expected output for the root folder with the config file referenced.
400+
# └── test_a.py
401+
# └── test_a_function: success
402+
config_file_pytest_expected_execution_output = {
403+
"tests/test_a.py::test_a_function": {
404+
"test": "tests/test_a.py::test_a_function",
405+
"outcome": "success",
406+
"message": None,
407+
"traceback": None,
408+
"subtest": None,
409+
}
410+
}

pythonFiles/tests/pytestadapter/test_discovery.py

-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ def test_pytest_root_dir():
165165
[
166166
"--collect-only",
167167
rd,
168-
# "-c",
169-
# "tests/pytest.ini",
170168
],
171169
TEST_DATA_PATH / "root",
172170
)

pythonFiles/tests/pytestadapter/test_execution.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
3+
import json
34
import os
45
import shutil
56

67
import pytest
78
from tests.pytestadapter import expected_execution_test_output
89

9-
from .helpers import TEST_DATA_PATH, runner
10+
from .helpers import TEST_DATA_PATH, runner, runner_with_cwd
11+
12+
13+
def test_config_file():
14+
"""Test pytest execution when a config file is specified."""
15+
args = ["-c", "tests/pytest.ini", "tests/test_a.py::test_a_function"]
16+
new_cwd = TEST_DATA_PATH / "root"
17+
actual = runner_with_cwd(args, new_cwd)
18+
expected_const = (
19+
expected_execution_test_output.config_file_pytest_expected_execution_output
20+
)
21+
assert actual
22+
assert len(actual) == len(expected_const)
23+
actual_result_dict = dict()
24+
for a in actual:
25+
assert all(item in a for item in ("status", "cwd", "result"))
26+
assert a["status"] == "success"
27+
assert a["cwd"] == os.fspath(new_cwd)
28+
actual_result_dict.update(a["result"])
29+
assert actual_result_dict == expected_const
30+
31+
32+
def test_rootdir_specified():
33+
"""Test pytest execution when a --rootdir is specified."""
34+
rd = f"--rootdir={TEST_DATA_PATH / 'root' / 'tests'}"
35+
args = [rd, "tests/test_a.py::test_a_function"]
36+
new_cwd = TEST_DATA_PATH / "root"
37+
actual = runner_with_cwd(args, new_cwd)
38+
expected_const = (
39+
expected_execution_test_output.config_file_pytest_expected_execution_output
40+
)
41+
assert actual
42+
assert len(actual) == len(expected_const)
43+
actual_result_dict = dict()
44+
for a in actual:
45+
assert all(item in a for item in ("status", "cwd", "result"))
46+
assert a["status"] == "success"
47+
assert a["cwd"] == os.fspath(new_cwd)
48+
actual_result_dict.update(a["result"])
49+
assert actual_result_dict == expected_const
1050

1151

1252
def test_syntax_error_execution(tmp_path):

pythonFiles/vscode_pytest/__init__.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def pytest_exception_interact(node, call, report):
8787
report_value = "error"
8888
if call.excinfo.typename == "AssertionError":
8989
report_value = "failure"
90-
node_id = str(node.nodeid)
90+
node_id = get_workspace_node_id(str(node.nodeid))
9191
if node_id not in collected_tests_so_far:
9292
collected_tests_so_far.append(node_id)
9393
item_result = create_test_outcome(
@@ -106,6 +106,14 @@ def pytest_exception_interact(node, call, report):
106106
)
107107

108108

109+
def get_workspace_node_id(testid: str):
110+
id = testid
111+
global RELATIVE_INVOCATION_PATH
112+
if RELATIVE_INVOCATION_PATH:
113+
id = str(pathlib.Path(RELATIVE_INVOCATION_PATH) / testid)
114+
return id
115+
116+
109117
def pytest_keyboard_interrupt(excinfo):
110118
"""A pytest hook that is called when a keyboard interrupt is raised.
111119
@@ -130,15 +138,15 @@ class TestOutcome(Dict):
130138

131139

132140
def create_test_outcome(
133-
test: str,
141+
testid: str,
134142
outcome: str,
135143
message: Union[str, None],
136144
traceback: Union[str, None],
137145
subtype: Optional[str] = None,
138146
) -> TestOutcome:
139147
"""A function that creates a TestOutcome object."""
140148
return TestOutcome(
141-
test=test,
149+
test=testid,
142150
outcome=outcome,
143151
message=message,
144152
traceback=traceback, # TODO: traceback
@@ -193,7 +201,7 @@ def pytest_report_teststatus(report, config):
193201
elif report.failed:
194202
report_value = "failure"
195203
message = report.longreprtext
196-
node_id = str(report.nodeid)
204+
node_id = get_workspace_node_id(str(report.nodeid))
197205
if node_id not in collected_tests_so_far:
198206
collected_tests_so_far.append(node_id)
199207
item_result = create_test_outcome(
@@ -222,7 +230,7 @@ def pytest_report_teststatus(report, config):
222230
def pytest_runtest_protocol(item, nextitem):
223231
skipped = check_skipped_wrapper(item)
224232
if skipped:
225-
node_id = str(item.nodeid)
233+
node_id = get_workspace_node_id(str(item.nodeid))
226234
report_value = "skipped"
227235
cwd = pathlib.Path.cwd()
228236
if node_id not in collected_tests_so_far:
@@ -480,10 +488,7 @@ def create_test_node(
480488
test_case_loc: str = (
481489
str(test_case.location[1] + 1) if (test_case.location[1] is not None) else ""
482490
)
483-
id = test_case.nodeid
484-
global RELATIVE_INVOCATION_PATH
485-
if RELATIVE_INVOCATION_PATH:
486-
id = str(pathlib.Path(RELATIVE_INVOCATION_PATH) / test_case.nodeid)
491+
id = get_workspace_node_id(test_case.nodeid)
487492
return {
488493
"name": test_case.name,
489494
"path": get_node_path(test_case),

0 commit comments

Comments
 (0)