Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 713007f

Browse files
authored
correct discovery on unittest skip at file level (microsoft#21665)
given a file called skip_test_file_node.py that has `raise SkipTest(".....")` this should appear in the sidebar with no children. The bug is that currently it shows a "unittest" node that gives "loader" and other incorrect nodes below it.
1 parent be334bd commit 713007f

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from unittest import SkipTest
5+
6+
raise SkipTest("This is unittest.SkipTest calling")
7+
8+
9+
def test_example():
10+
assert 1 == 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import unittest
5+
6+
7+
def add(x, y):
8+
return x + y
9+
10+
11+
class SimpleTest(unittest.TestCase):
12+
@unittest.skip("demonstrating skipping")
13+
def testadd1(self):
14+
self.assertEquals(add(4, 5), 9)
15+
16+
17+
if __name__ == "__main__":
18+
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import os
5+
from unittestadapter.utils import TestNodeTypeEnum
6+
from .helpers import TEST_DATA_PATH
7+
8+
skip_unittest_folder_discovery_output = {
9+
"path": os.fspath(TEST_DATA_PATH / "unittest_skip"),
10+
"name": "unittest_skip",
11+
"type_": TestNodeTypeEnum.folder,
12+
"children": [
13+
{
14+
"path": os.fspath(
15+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_file.py"
16+
),
17+
"name": "unittest_skip_file.py",
18+
"type_": TestNodeTypeEnum.file,
19+
"children": [],
20+
"id_": os.fspath(
21+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_file.py"
22+
),
23+
},
24+
{
25+
"path": os.fspath(
26+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
27+
),
28+
"name": "unittest_skip_function.py",
29+
"type_": TestNodeTypeEnum.file,
30+
"children": [
31+
{
32+
"path": os.fspath(
33+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
34+
),
35+
"name": "SimpleTest",
36+
"type_": TestNodeTypeEnum.class_,
37+
"children": [
38+
{
39+
"name": "testadd1",
40+
"path": os.fspath(
41+
TEST_DATA_PATH
42+
/ "unittest_skip"
43+
/ "unittest_skip_function.py"
44+
),
45+
"lineno": "13",
46+
"type_": TestNodeTypeEnum.test,
47+
"id_": os.fspath(
48+
TEST_DATA_PATH
49+
/ "unittest_skip"
50+
/ "unittest_skip_function.py"
51+
)
52+
+ "\\SimpleTest\\testadd1",
53+
"runID": "unittest_skip_function.SimpleTest.testadd1",
54+
}
55+
],
56+
"id_": os.fspath(
57+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
58+
)
59+
+ "\\SimpleTest",
60+
}
61+
],
62+
"id_": os.fspath(
63+
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
64+
),
65+
},
66+
],
67+
"id_": os.fspath(TEST_DATA_PATH / "unittest_skip"),
68+
}

pythonFiles/tests/unittestadapter/test_discovery.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
parse_discovery_cli_args,
1313
)
1414
from unittestadapter.utils import TestNodeTypeEnum, parse_unittest_args
15-
15+
from . import expected_discovery_test_output
1616
from .helpers import TEST_DATA_PATH, is_same_tree
1717

1818

@@ -214,3 +214,22 @@ def test_error_discovery() -> None:
214214
assert actual["status"] == "error"
215215
assert is_same_tree(expected, actual.get("tests"))
216216
assert len(actual.get("error", [])) == 1
217+
218+
219+
def test_unit_skip() -> None:
220+
"""The discover_tests function should return a dictionary with a "success" status, a uuid, no errors, and test tree.
221+
if unittest discovery was performed and found a test in one file marked as skipped and another file marked as skipped.
222+
"""
223+
start_dir = os.fsdecode(TEST_DATA_PATH / "unittest_skip")
224+
pattern = "unittest_*"
225+
226+
uuid = "some-uuid"
227+
actual = discover_tests(start_dir, pattern, None, uuid)
228+
229+
assert actual["status"] == "success"
230+
assert "tests" in actual
231+
assert is_same_tree(
232+
actual.get("tests"),
233+
expected_discovery_test_output.skip_unittest_folder_discovery_output,
234+
)
235+
assert "error" not in actual

pythonFiles/unittestadapter/utils.py

+8
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ def build_test_tree(
159159
test_id = test_case.id()
160160
if test_id.startswith("unittest.loader._FailedTest"):
161161
error.append(str(test_case._exception)) # type: ignore
162+
elif test_id.startswith("unittest.loader.ModuleSkipped"):
163+
components = test_id.split(".")
164+
class_name = f"{components[-1]}.py"
165+
# Find/build class node.
166+
file_path = os.fsdecode(os.path.join(directory_path, class_name))
167+
current_node = get_child_node(
168+
class_name, file_path, TestNodeTypeEnum.file, root
169+
)
162170
else:
163171
# Get the static test path components: filename, class name and function name.
164172
components = test_id.split(".")

0 commit comments

Comments
 (0)