Skip to content

Commit

Permalink
correct discovery on unittest skip at file level (#21665)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eleanorjboyd authored Jul 19, 2023
1 parent be334bd commit 713007f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from unittest import SkipTest

raise SkipTest("This is unittest.SkipTest calling")


def test_example():
assert 1 == 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import unittest


def add(x, y):
return x + y


class SimpleTest(unittest.TestCase):
@unittest.skip("demonstrating skipping")
def testadd1(self):
self.assertEquals(add(4, 5), 9)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
from unittestadapter.utils import TestNodeTypeEnum
from .helpers import TEST_DATA_PATH

skip_unittest_folder_discovery_output = {
"path": os.fspath(TEST_DATA_PATH / "unittest_skip"),
"name": "unittest_skip",
"type_": TestNodeTypeEnum.folder,
"children": [
{
"path": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_file.py"
),
"name": "unittest_skip_file.py",
"type_": TestNodeTypeEnum.file,
"children": [],
"id_": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_file.py"
),
},
{
"path": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
),
"name": "unittest_skip_function.py",
"type_": TestNodeTypeEnum.file,
"children": [
{
"path": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
),
"name": "SimpleTest",
"type_": TestNodeTypeEnum.class_,
"children": [
{
"name": "testadd1",
"path": os.fspath(
TEST_DATA_PATH
/ "unittest_skip"
/ "unittest_skip_function.py"
),
"lineno": "13",
"type_": TestNodeTypeEnum.test,
"id_": os.fspath(
TEST_DATA_PATH
/ "unittest_skip"
/ "unittest_skip_function.py"
)
+ "\\SimpleTest\\testadd1",
"runID": "unittest_skip_function.SimpleTest.testadd1",
}
],
"id_": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
)
+ "\\SimpleTest",
}
],
"id_": os.fspath(
TEST_DATA_PATH / "unittest_skip" / "unittest_skip_function.py"
),
},
],
"id_": os.fspath(TEST_DATA_PATH / "unittest_skip"),
}
21 changes: 20 additions & 1 deletion pythonFiles/tests/unittestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
parse_discovery_cli_args,
)
from unittestadapter.utils import TestNodeTypeEnum, parse_unittest_args

from . import expected_discovery_test_output
from .helpers import TEST_DATA_PATH, is_same_tree


Expand Down Expand Up @@ -214,3 +214,22 @@ def test_error_discovery() -> None:
assert actual["status"] == "error"
assert is_same_tree(expected, actual.get("tests"))
assert len(actual.get("error", [])) == 1


def test_unit_skip() -> None:
"""The discover_tests function should return a dictionary with a "success" status, a uuid, no errors, and test tree.
if unittest discovery was performed and found a test in one file marked as skipped and another file marked as skipped.
"""
start_dir = os.fsdecode(TEST_DATA_PATH / "unittest_skip")
pattern = "unittest_*"

uuid = "some-uuid"
actual = discover_tests(start_dir, pattern, None, uuid)

assert actual["status"] == "success"
assert "tests" in actual
assert is_same_tree(
actual.get("tests"),
expected_discovery_test_output.skip_unittest_folder_discovery_output,
)
assert "error" not in actual
8 changes: 8 additions & 0 deletions pythonFiles/unittestadapter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ def build_test_tree(
test_id = test_case.id()
if test_id.startswith("unittest.loader._FailedTest"):
error.append(str(test_case._exception)) # type: ignore
elif test_id.startswith("unittest.loader.ModuleSkipped"):
components = test_id.split(".")
class_name = f"{components[-1]}.py"
# Find/build class node.
file_path = os.fsdecode(os.path.join(directory_path, class_name))
current_node = get_child_node(
class_name, file_path, TestNodeTypeEnum.file, root
)
else:
# Get the static test path components: filename, class name and function name.
components = test_id.split(".")
Expand Down

0 comments on commit 713007f

Please sign in to comment.