Skip to content
Merged
Empty file.
6 changes: 6 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/root/tests/test_a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


def test_a_function(): # test_marker--test_a_function
assert True
6 changes: 6 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/root/tests/test_b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.


def test_b_function(): # test_marker--test_b_function
assert True
56 changes: 56 additions & 0 deletions pythonFiles/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,59 @@
],
"id_": TEST_DATA_PATH_STR,
}

tests_path = (
"/Users/eleanorboyd/vscode-python/pythonFiles/tests/pytestadapter/.data/root/tests"
)
# This is the expected output for the root folder tests.
# └── tests
# └── test_a.py
# └── test_a_function
# └── test_b.py
# └── test_b_function
root_with_config_expected_output = {
"name": "tests",
"path": tests_path,
"type_": "folder",
"children": [
{
"name": "test_a.py",
"path": os.fspath(os.path.join(tests_path, "test_a.py")),
"type_": "file",
"id_": os.fspath(os.path.join(tests_path, "test_a.py")),
"children": [
{
"name": "test_a_function",
"path": os.fspath(os.path.join(tests_path, "test_a.py")),
"lineno": find_test_line_number(
"test_a_function",
os.fspath(os.path.join(tests_path, "test_a.py")),
),
"type_": "test",
"id_": "tests/test_a.py::test_a_function",
"runID": "tests/test_a.py::test_a_function",
}
],
},
{
"name": "test_b.py",
"path": os.fspath(os.path.join(tests_path, "test_b.py")),
"type_": "file",
"id_": os.fspath(os.path.join(tests_path, "test_b.py")),
"children": [
{
"name": "test_b_function",
"path": os.fspath(os.path.join(tests_path, "test_b.py")),
"lineno": find_test_line_number(
"test_b_function",
os.fspath(os.path.join(tests_path, "test_b.py")),
),
"type_": "test",
"id_": "tests/test_b.py::test_b_function",
"runID": "tests/test_b.py::test_b_function",
}
],
},
],
"id_": tests_path,
}
9 changes: 8 additions & 1 deletion pythonFiles/tests/pytestadapter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ def process_rpc_json(data: str) -> List[Dict[str, Any]]:


def runner(args: List[str]) -> Optional[List[Dict[str, Any]]]:
"""Run the pytest discovery and return the JSON data from the server."""
return runner_with_cwd(args, TEST_DATA_PATH)


def runner_with_cwd(
args: List[str], path: pathlib.Path
) -> Optional[List[Dict[str, Any]]]:
"""Run the pytest discovery and return the JSON data from the server."""
process_args: List[str] = [
sys.executable,
Expand Down Expand Up @@ -134,7 +141,7 @@ def runner(args: List[str]) -> Optional[List[Dict[str, Any]]]:

t2 = threading.Thread(
target=_run_test_code,
args=(process_args, env, TEST_DATA_PATH, completed),
args=(process_args, env, path, completed),
)
t2.start()

Expand Down
54 changes: 53 additions & 1 deletion pythonFiles/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest

from . import expected_discovery_test_output
from .helpers import TEST_DATA_PATH, runner
from .helpers import TEST_DATA_PATH, runner, runner_with_cwd


def test_import_error(tmp_path):
Expand Down Expand Up @@ -153,3 +153,55 @@ def test_pytest_collect(file, expected_const):
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert actual["tests"] == expected_const


def test_pytest_root_dir():
"""
Test to test pytest discovery with the command line arg --rootdir specified to be a subfolder
of the workspace root. Discovery should succeed and testids should be relative to workspace root.
"""
rd = f"--rootdir={TEST_DATA_PATH / 'root' / 'tests'}"
actual = runner_with_cwd(
[
"--collect-only",
rd,
# "-c",
# "tests/pytest.ini",
],
TEST_DATA_PATH / "root",
)
if actual:
actual = actual[0]
assert actual
assert all(item in actual for item in ("status", "cwd", "tests"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH / "root")
assert (
actual["tests"]
== expected_discovery_test_output.root_with_config_expected_output
)


def test_pytest_config_file():
"""
Test to test pytest discovery with the command line arg -c with a specified config file which
changes the workspace root. Discovery should succeed and testids should be relative to workspace root.
"""
actual = runner_with_cwd(
[
"--collect-only",
"-c",
"tests/pytest.ini",
],
TEST_DATA_PATH / "root",
)
if actual:
actual = actual[0]
assert actual
assert all(item in actual for item in ("status", "cwd", "tests"))
assert actual["status"] == "success"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH / "root")
assert (
actual["tests"]
== expected_discovery_test_output.root_with_config_expected_output
)
17 changes: 15 additions & 2 deletions pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,21 @@ class testRunResultDict(Dict[str, Dict[str, TestOutcome]]):


IS_DISCOVERY = False
RELATIVE_INVOCATION_PATH = ""


def pytest_load_initial_conftests(early_config, parser, args):
if "--collect-only" in args:
global IS_DISCOVERY
IS_DISCOVERY = True
invocation_dir = early_config.invocation_params.dir
root = early_config.rootpath
if invocation_dir != root:
try:
global RELATIVE_INVOCATION_PATH
RELATIVE_INVOCATION_PATH = root.relative_to(invocation_dir)
except:
pass


collected_tests_so_far = list()
Expand Down Expand Up @@ -471,13 +480,17 @@ def create_test_node(
test_case_loc: str = (
str(test_case.location[1] + 1) if (test_case.location[1] is not None) else ""
)
id = test_case.nodeid
global RELATIVE_INVOCATION_PATH
if RELATIVE_INVOCATION_PATH:
id = str(pathlib.Path(RELATIVE_INVOCATION_PATH) / test_case.nodeid)
return {
"name": test_case.name,
"path": get_node_path(test_case),
"lineno": test_case_loc,
"type_": "test",
"id_": test_case.nodeid,
"runID": test_case.nodeid,
"id_": id,
"runID": id,
}


Expand Down