Skip to content

Commit

Permalink
Support pytest parameterized tests spanning multiple classes when cal…
Browse files Browse the repository at this point in the history
…ling the same setup function (#23535)

fixes #23527
  • Loading branch information
eleanorjboyd authored Jun 4, 2024
1 parent 838807d commit 629ca42
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
16 changes: 16 additions & 0 deletions python_files/tests/pytestadapter/.data/test_param_span_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest


@pytest.fixture(scope="function", params=[1, 2])
def setup(request):
return request.param


class TestClass1:
def test_method1(self, setup): # test_marker--TestClass1::test_method1
assert 1 == 1


class TestClass2:
def test_method1(self, setup): # test_marker--TestClass2::test_method1
assert 2 == 2
123 changes: 122 additions & 1 deletion python_files/tests/pytestadapter/expected_discovery_test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,125 @@
"id_": TEST_DATA_PATH_STR,
}

print(param_same_name_expected_output)
test_param_span_class_expected_output = {
"name": ".data",
"path": TEST_DATA_PATH_STR,
"type_": "folder",
"children": [
{
"name": "test_param_span_class.py",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"type_": "file",
"id_": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"children": [
{
"name": "TestClass1",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"type_": "class",
"children": [
{
"name": "test_method1",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"type_": "function",
"children": [
{
"name": "[1]",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"lineno": find_test_line_number(
"TestClass1::test_method1",
os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
),
"type_": "test",
"id_": get_absolute_test_id(
"test_param_span_class.py::TestClass1::test_method1[1]",
TEST_DATA_PATH / "test_param_span_class.py",
),
"runID": get_absolute_test_id(
"test_param_span_class.py::TestClass1::test_method1[1]",
TEST_DATA_PATH / "test_param_span_class.py",
),
},
{
"name": "[2]",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"lineno": find_test_line_number(
"TestClass1::test_method1",
os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
),
"type_": "test",
"id_": get_absolute_test_id(
"test_param_span_class.py::TestClass1::test_method1[2]",
TEST_DATA_PATH / "test_param_span_class.py",
),
"runID": get_absolute_test_id(
"test_param_span_class.py::TestClass1::test_method1[2]",
TEST_DATA_PATH / "test_param_span_class.py",
),
},
],
"id_": os.fspath(
TEST_DATA_PATH
/ "test_param_span_class.py::TestClass1::test_method1"
),
}
],
"id_": "test_param_span_class.py::TestClass1",
},
{
"name": "TestClass2",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"type_": "class",
"children": [
{
"name": "test_method1",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"type_": "function",
"children": [
{
"name": "[1]",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"lineno": find_test_line_number(
"TestClass2::test_method1",
os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
),
"type_": "test",
"id_": get_absolute_test_id(
"test_param_span_class.py::TestClass2::test_method1[1]",
TEST_DATA_PATH / "test_param_span_class.py",
),
"runID": get_absolute_test_id(
"test_param_span_class.py::TestClass2::test_method1[1]",
TEST_DATA_PATH / "test_param_span_class.py",
),
},
{
"name": "[2]",
"path": os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
"lineno": find_test_line_number(
"TestClass2::test_method1",
os.fspath(TEST_DATA_PATH / "test_param_span_class.py"),
),
"type_": "test",
"id_": get_absolute_test_id(
"test_param_span_class.py::TestClass2::test_method1[2]",
TEST_DATA_PATH / "test_param_span_class.py",
),
"runID": get_absolute_test_id(
"test_param_span_class.py::TestClass2::test_method1[2]",
TEST_DATA_PATH / "test_param_span_class.py",
),
},
],
"id_": os.fspath(
TEST_DATA_PATH
/ "test_param_span_class.py::TestClass2::test_method1"
),
}
],
"id_": "test_param_span_class.py::TestClass2",
},
],
}
],
"id_": TEST_DATA_PATH_STR,
}
4 changes: 4 additions & 0 deletions python_files/tests/pytestadapter/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def test_parameterized_error_collect():
@pytest.mark.parametrize(
"file, expected_const",
[
(
"test_param_span_class.py",
expected_discovery_test_output.test_param_span_class_expected_output,
),
(
"test_multi_class_nest.py",
expected_discovery_test_output.nested_classes_expected_test_output,
Expand Down
3 changes: 2 additions & 1 deletion python_files/vscode_pytest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ def build_test_tree(session: pytest.Session) -> TestNode:
function_name, get_node_path(test_case), parent_id
)
function_nodes_dict[parent_id] = function_test_node
function_test_node["children"].append(test_node)
if test_node not in function_test_node["children"]:
function_test_node["children"].append(test_node)
# Check if the parent node of the function is file, if so create/add to this file node.
if isinstance(test_case.parent, pytest.File):
try:
Expand Down

0 comments on commit 629ca42

Please sign in to comment.