Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support yaml tests #22683

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
"type": "python",
"request": "launch",
"module": "pytest",
"args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter"],
"args": ["${workspaceFolder}/pythonFiles/tests/pytestadapter", "-vv"],
"justMyCode": true
}
],
Expand Down
8 changes: 8 additions & 0 deletions pythonFiles/tests/pytestadapter/.data/test_yaml.yaml
Original file line number Diff line number Diff line change
@@ -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]?"
31 changes: 31 additions & 0 deletions pythonFiles/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,37 @@
],
"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": "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
),
"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 = {
Expand Down
23 changes: 18 additions & 5 deletions pythonFiles/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -123,6 +124,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,
Expand Down Expand Up @@ -180,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)
print("os diff list EJFB", os.listdir(TEST_DATA_PATH))
assert path_fs.exists()
actual = runner(
[
"--collect-only",
os.fspath(TEST_DATA_PATH / file),
os.fspath(path_fs),
]
)

Expand All @@ -192,10 +198,17 @@ def test_pytest_collect(file, expected_const):
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 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("assertion error occured, the actual item value is: \n")
print(json.dumps(actual_item, indent=4), "\n")
raise


def test_pytest_root_dir():
Expand Down
4 changes: 3 additions & 1 deletion pythonFiles/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,11 @@ 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"
)

absolute_test_id = get_absolute_test_id(test_case.nodeid, get_node_path(test_case))
return {
"name": test_case.name,
Expand Down
5 changes: 5 additions & 0 deletions src/client/testing/testController/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading