From 7e2040a43011af6d8eb2e2a475ca90e59bfe057c Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 19 Dec 2023 14:43:48 -0500 Subject: [PATCH 1/7] support yaml tests --- .../tests/pytestadapter/.data/test_yaml.yaml | 8 +++++ .../expected_discovery_test_output.py | 30 +++++++++++++++++++ .../tests/pytestadapter/test_discovery.py | 1 + pythonFiles/vscode_pytest/__init__.py | 4 +++ .../testing/testController/common/utils.ts | 5 ++++ 5 files changed, 48 insertions(+) create mode 100644 pythonFiles/tests/pytestadapter/.data/test_yaml.yaml diff --git a/pythonFiles/tests/pytestadapter/.data/test_yaml.yaml b/pythonFiles/tests/pytestadapter/.data/test_yaml.yaml new file mode 100644 index 000000000000..344c4e273cfc --- /dev/null +++ b/pythonFiles/tests/pytestadapter/.data/test_yaml.yaml @@ -0,0 +1,8 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +- case: test_integer_type + main: | + reveal_type(123) + out: | + main:1: note: Revealed type is "Literal[123]?" diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index d4e91f56b5fe..42158fdff924 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -886,6 +886,36 @@ ], "id_": os.fspath(tests_path), } + +TEST_YAML_PATH = TEST_DATA_PATH / "test_yaml.yaml" +yaml_expected_output = { + "name": ".data", + "path": str(TEST_DATA_PATH), + "type_": "folder", + "children": [ + { + "name": "test_yaml.yaml", + "path": str(TEST_YAML_PATH), + "type_": "file", + "id_": str(TEST_YAML_PATH), + "children": [ + { + "name": "test_integer_type", + "path": str(TEST_YAML_PATH), + "lineno": "6", + "type_": "test", + "id_": get_absolute_test_id( + "test_yaml.yaml::test_integer_type", TEST_YAML_PATH + ), + "runID": get_absolute_test_id( + "test_yaml.yaml::test_integer_type", TEST_YAML_PATH + ), + } + ], + } + ], + "id_": str(TEST_DATA_PATH), +} TEST_MULTI_CLASS_NEST_PATH = TEST_DATA_PATH / "test_multi_class_nest.py" nested_classes_expected_test_output = { diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 2630ddef68b0..1966c0ff684a 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -123,6 +123,7 @@ def test_parameterized_error_collect(): @pytest.mark.parametrize( "file, expected_const", [ + ("test_yaml.yaml", expected_discovery_test_output.yaml_expected_output), ( "test_multi_class_nest.py", expected_discovery_test_output.nested_classes_expected_test_output, diff --git a/pythonFiles/vscode_pytest/__init__.py b/pythonFiles/vscode_pytest/__init__.py index a565fbf930a6..da50aa6761bc 100644 --- a/pythonFiles/vscode_pytest/__init__.py +++ b/pythonFiles/vscode_pytest/__init__.py @@ -521,6 +521,10 @@ def create_test_node( test_case_loc: str = ( str(test_case.location[1] + 1) if (test_case.location[1] is not None) else "" ) + # This check is for yaml tests, which do not have a location but do have a starting_lineno. + if test_case_loc == "" and test_case.starting_lineno is not None: + test_case_loc = str(test_case.starting_lineno + 1) + absolute_test_id = get_absolute_test_id(test_case.nodeid, get_node_path(test_case)) return { "name": test_case.name, diff --git a/src/client/testing/testController/common/utils.ts b/src/client/testing/testController/common/utils.ts index 23ee881a405a..6ae14d91d264 100644 --- a/src/client/testing/testController/common/utils.ts +++ b/src/client/testing/testController/common/utils.ts @@ -255,6 +255,11 @@ export function populateTestTree( const testItem = testController.createTestItem(child.id_, child.name, Uri.file(child.path)); testItem.tags = [RunTestTag, DebugTestTag]; + // Check for a undefined value (here will be 0) and if found set lineno to 1 as a default. + if (Number(child.lineno) === 0) { + traceError(`Test item ${child.name} does not have a line number.`); + child.lineno = 1; + } const range = new Range( new Position(Number(child.lineno) - 1, 0), new Position(Number(child.lineno), 0), From 9727a1327391c62d0127ba6d11377e304d3c1eed Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Tue, 19 Dec 2023 14:50:39 -0500 Subject: [PATCH 2/7] fix merge conflicts --- .../tests/pytestadapter/expected_discovery_test_output.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index 42158fdff924..e829c5e9d915 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -916,6 +916,7 @@ ], "id_": str(TEST_DATA_PATH), } + TEST_MULTI_CLASS_NEST_PATH = TEST_DATA_PATH / "test_multi_class_nest.py" nested_classes_expected_test_output = { From 21e4d57b62f25d0fd827b1bc2acab14157fc51e9 Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 31 Jan 2024 11:17:02 -0800 Subject: [PATCH 3/7] fix for yaml --- .vscode/launch.json | 2 +- .../tests/pytestadapter/expected_discovery_test_output.py | 2 +- pythonFiles/vscode_pytest/__init__.py | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 82981a93305d..a3b74e1ebcc4 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -260,7 +260,7 @@ "type": "python", "request": "launch", "module": "pytest", - "args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter"], + "args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter", "-vv"], "justMyCode": true } ], diff --git a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py index e829c5e9d915..33e85d72f8ac 100644 --- a/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py +++ b/pythonFiles/tests/pytestadapter/expected_discovery_test_output.py @@ -902,7 +902,7 @@ { "name": "test_integer_type", "path": str(TEST_YAML_PATH), - "lineno": "6", + "lineno": "1", # Since the yaml test type is not in the pytest library (instead pytest-mypy-plugins), we will not support their line number but instead changing the default value to "1" to prevent failure. "type_": "test", "id_": get_absolute_test_id( "test_yaml.yaml::test_integer_type", TEST_YAML_PATH diff --git a/pythonFiles/vscode_pytest/__init__.py b/pythonFiles/vscode_pytest/__init__.py index da50aa6761bc..0655cdcfba2f 100644 --- a/pythonFiles/vscode_pytest/__init__.py +++ b/pythonFiles/vscode_pytest/__init__.py @@ -518,12 +518,10 @@ def create_test_node( Keyword arguments: test_case -- the pytest test case. """ + # If test case has no location, set it to 1 as a default so test node is valid. test_case_loc: str = ( - str(test_case.location[1] + 1) if (test_case.location[1] is not None) else "" + str(test_case.location[1] + 1) if (test_case.location[1] is not None) else "1" ) - # This check is for yaml tests, which do not have a location but do have a starting_lineno. - if test_case_loc == "" and test_case.starting_lineno is not None: - test_case_loc = str(test_case.starting_lineno + 1) absolute_test_id = get_absolute_test_id(test_case.nodeid, get_node_path(test_case)) return { From 0723933133adb031cc3ee74a27d92e6963bbefac Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 31 Jan 2024 11:32:31 -0800 Subject: [PATCH 4/7] add assertion error json dump for debugging --- .../tests/pytestadapter/test_discovery.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 1966c0ff684a..859f132f30a6 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +import json import os import shutil from typing import Any, Dict, List, Optional @@ -191,12 +192,18 @@ def test_pytest_collect(file, expected_const): assert actual actual_list: List[Dict[str, Any]] = actual if actual_list is not None: - assert actual_list.pop(-1).get("eot") - actual_item = actual_list.pop(0) - assert all(item in actual_item.keys() for item in ("status", "cwd", "error")) - assert actual_item.get("status") == "success" - assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH) - assert actual_item.get("tests") == expected_const + try: + assert actual_list.pop(-1).get("eot") + actual_item = actual_list.pop(0) + assert all( + item in actual_item.keys() for item in ("status", "cwd", "error") + ) + assert actual_item.get("status") == "success" + assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH) + assert actual_item.get("tests") == expected_const + except AssertionError: + print(json.dumps(actual_item, indent=4)) + raise def test_pytest_root_dir(): From 40e77972c44c078528dc1706a3d6e48de61aeece Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 31 Jan 2024 14:46:44 -0800 Subject: [PATCH 5/7] fix test unbound error handling --- pythonFiles/tests/pytestadapter/test_discovery.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 859f132f30a6..9d084db2d93a 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -192,9 +192,9 @@ def test_pytest_collect(file, expected_const): assert actual actual_list: List[Dict[str, Any]] = actual if actual_list is not None: + assert actual_list.pop(-1).get("eot") + actual_item = actual_list.pop(0) try: - assert actual_list.pop(-1).get("eot") - actual_item = actual_list.pop(0) assert all( item in actual_item.keys() for item in ("status", "cwd", "error") ) From ffac329174dce8aefd4b2deefe17ad0bd649d94c Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 31 Jan 2024 15:02:05 -0800 Subject: [PATCH 6/7] better error msgs --- pythonFiles/tests/pytestadapter/test_discovery.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 9d084db2d93a..91777049716d 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -182,10 +182,14 @@ def test_pytest_collect(file, expected_const): file -- a string with the file or folder to run pytest discovery on. expected_const -- the expected output from running pytest discovery on the file. """ + path_fs = TEST_DATA_PATH / file + print("PATH: EJFB", path_fs) + os.listdir(TEST_DATA_PATH) + assert path_fs.exists() actual = runner( [ "--collect-only", - os.fspath(TEST_DATA_PATH / file), + os.fspath(path_fs), ] ) @@ -202,7 +206,8 @@ def test_pytest_collect(file, expected_const): assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH) assert actual_item.get("tests") == expected_const except AssertionError: - print(json.dumps(actual_item, indent=4)) + print("assertion error occured, the actual item value is: \n") + print(json.dumps(actual_item, indent=4), "\n") raise From 81a8a69a5f90cb9f2f5a3512360981a710fa25fc Mon Sep 17 00:00:00 2001 From: eleanorjboyd Date: Wed, 31 Jan 2024 15:11:20 -0800 Subject: [PATCH 7/7] os list --- pythonFiles/tests/pytestadapter/test_discovery.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pythonFiles/tests/pytestadapter/test_discovery.py b/pythonFiles/tests/pytestadapter/test_discovery.py index 91777049716d..c75d6c496e0e 100644 --- a/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/pythonFiles/tests/pytestadapter/test_discovery.py @@ -184,7 +184,7 @@ def test_pytest_collect(file, expected_const): """ path_fs = TEST_DATA_PATH / file print("PATH: EJFB", path_fs) - os.listdir(TEST_DATA_PATH) + print("os diff list EJFB", os.listdir(TEST_DATA_PATH)) assert path_fs.exists() actual = runner( [