forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_discovery.py
More file actions
151 lines (133 loc) · 5.36 KB
/
test_discovery.py
File metadata and controls
151 lines (133 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import shutil
from typing import Any, Dict, List, Optional
import pytest
from . import expected_discovery_test_output
from .helpers import TEST_DATA_PATH, runner
def test_import_error(tmp_path):
"""Test pytest discovery on a file that has a pytest marker but does not import pytest.
Copies the contents of a .txt file to a .py file in the temporary directory
to then run pytest discovery on.
The json should still be returned but the errors list should be present.
Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
# Saving some files as .txt to avoid that file displaying a syntax error for
# the extension as a whole. Instead, rename it before running this test
# in order to test the error handling.
file_path = TEST_DATA_PATH / "error_pytest_import.txt"
temp_dir = tmp_path / "temp_data"
temp_dir.mkdir()
p = temp_dir / "error_pytest_import.py"
shutil.copyfile(file_path, p)
actual_list: Optional[List[Dict[str, Any]]] = runner(
["--collect-only", os.fspath(p)]
)
assert actual_list
for actual in actual_list:
assert all(item in actual for item in ("status", "cwd", "error"))
assert actual["status"] == "error"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert len(actual["error"]) == 2
def test_syntax_error(tmp_path):
"""Test pytest discovery on a file that has a syntax error.
Copies the contents of a .txt file to a .py file in the temporary directory
to then run pytest discovery on.
The json should still be returned but the errors list should be present.
Keyword arguments:
tmp_path -- pytest fixture that creates a temporary directory.
"""
# Saving some files as .txt to avoid that file displaying a syntax error for
# the extension as a whole. Instead, rename it before running this test
# in order to test the error handling.
file_path = TEST_DATA_PATH / "error_syntax_discovery.txt"
temp_dir = tmp_path / "temp_data"
temp_dir.mkdir()
p = temp_dir / "error_syntax_discovery.py"
shutil.copyfile(file_path, p)
actual = runner(["--collect-only", os.fspath(p)])
if actual:
actual = actual[0]
assert actual
assert all(item in actual for item in ("status", "cwd", "error"))
assert actual["status"] == "error"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert len(actual["error"]) == 2
def test_parameterized_error_collect():
"""Tests pytest discovery on specific file that incorrectly uses parametrize.
The json should still be returned but the errors list should be present.
"""
file_path_str = "error_parametrize_discovery.py"
actual = runner(["--collect-only", file_path_str])
if actual:
actual = actual[0]
assert all(item in actual for item in ("status", "cwd", "error"))
assert actual["status"] == "error"
assert actual["cwd"] == os.fspath(TEST_DATA_PATH)
assert len(actual["error"]) == 2
@pytest.mark.parametrize(
"file, expected_const",
[
(
"param_same_name",
expected_discovery_test_output.param_same_name_expected_output,
),
(
"parametrize_tests.py",
expected_discovery_test_output.parametrize_tests_expected_output,
),
(
"empty_discovery.py",
expected_discovery_test_output.empty_discovery_pytest_expected_output,
),
(
"simple_pytest.py",
expected_discovery_test_output.simple_discovery_pytest_expected_output,
),
(
"unittest_pytest_same_file.py",
expected_discovery_test_output.unit_pytest_same_file_discovery_expected_output,
),
(
"unittest_folder",
expected_discovery_test_output.unittest_folder_discovery_expected_output,
),
(
"dual_level_nested_folder",
expected_discovery_test_output.dual_level_nested_folder_expected_output,
),
(
"folder_a",
expected_discovery_test_output.double_nested_folder_expected_output,
),
(
"text_docstring.txt",
expected_discovery_test_output.doctest_pytest_expected_output,
),
],
)
def test_pytest_collect(file, expected_const):
"""
Test to test pytest discovery on a variety of test files/ folder structures.
Uses variables from expected_discovery_test_output.py to store the expected dictionary return.
Only handles discovery and therefore already contains the arg --collect-only.
All test discovery will succeed, be in the correct cwd, and match expected test output.
Keyword arguments:
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.
"""
actual = runner(
[
"--collect-only",
os.fspath(TEST_DATA_PATH / file),
]
)
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)
assert actual["tests"] == expected_const