Skip to content

Commit 55cd2e0

Browse files
authored
add test for import pytest error (#21472)
fixes #21470
1 parent 2e936ef commit 55cd2e0

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
@pytest.mark.parametrize("num", range(1, 89))
5+
def test_odd_even(num):
6+
assert True

pythonFiles/tests/pytestadapter/test_discovery.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010
from .helpers import TEST_DATA_PATH, runner
1111

1212

13+
def test_import_error(tmp_path):
14+
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.
15+
16+
Copies the contents of a .txt file to a .py file in the temporary directory
17+
to then run pytest discovery on.
18+
19+
The json should still be returned but the errors list should be present.
20+
21+
Keyword arguments:
22+
tmp_path -- pytest fixture that creates a temporary directory.
23+
"""
24+
# Saving some files as .txt to avoid that file displaying a syntax error for
25+
# the extension as a whole. Instead, rename it before running this test
26+
# in order to test the error handling.
27+
file_path = TEST_DATA_PATH / "error_pytest_import.txt"
28+
temp_dir = tmp_path / "temp_data"
29+
temp_dir.mkdir()
30+
p = temp_dir / "error_pytest_import.py"
31+
shutil.copyfile(file_path, p)
32+
actual_list: Optional[List[Dict[str, Any]]] = runner(
33+
["--collect-only", os.fspath(p)]
34+
)
35+
assert actual_list
36+
for actual in actual_list:
37+
assert all(item in actual for item in ("status", "cwd", "error"))
38+
assert actual["status"] == "error"
39+
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
40+
assert len(actual["error"]) == 2
41+
42+
1343
def test_syntax_error(tmp_path):
1444
"""Test pytest discovery on a file that has a syntax error.
1545

pythonFiles/vscode_pytest/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def pytest_keyboard_interrupt(excinfo):
7979
Keyword arguments:
8080
excinfo -- the exception information of type ExceptionInfo.
8181
"""
82-
# The function execonly() returns the exception as a string.
82+
# The function exconly() returns the exception as a string.
8383
ERRORS.append(excinfo.exconly())
8484

8585

0 commit comments

Comments
 (0)