Skip to content

Commit c4c05a6

Browse files
authored
update tree comparison for tests to be order independent for children (#22832)
following the introduction of pytest 8, the order in which children were listed changed. Since the order is not important, this updates the tests to make the tests not consider order of children when comparing actual and expected outcomes of test runs.
1 parent 4030717 commit c4c05a6

File tree

9 files changed

+56
-36
lines changed

9 files changed

+56
-36
lines changed

pythonFiles/tests/pytestadapter/expected_discovery_test_output.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@
317317
# └── test_top_folder.py
318318
# └── test_top_function_t
319319
# └── test_top_function_f
320-
# └── z_nested_folder_one
320+
# └── nested_folder_one
321321
# └── test_bottom_folder.py
322322
# └── test_bottom_function_t
323323
# └── test_bottom_function_f
@@ -326,14 +326,14 @@
326326
TEST_DATA_PATH / "dual_level_nested_folder" / "test_top_folder.py"
327327
)
328328

329-
test_z_nested_folder_one_path = (
330-
TEST_DATA_PATH / "dual_level_nested_folder" / "z_nested_folder_one"
329+
test_nested_folder_one_path = (
330+
TEST_DATA_PATH / "dual_level_nested_folder" / "nested_folder_one"
331331
)
332332

333333
test_bottom_folder_path = (
334334
TEST_DATA_PATH
335335
/ "dual_level_nested_folder"
336-
/ "z_nested_folder_one"
336+
/ "nested_folder_one"
337337
/ "test_bottom_folder.py"
338338
)
339339

@@ -392,10 +392,10 @@
392392
],
393393
},
394394
{
395-
"name": "z_nested_folder_one",
396-
"path": os.fspath(test_z_nested_folder_one_path),
395+
"name": "nested_folder_one",
396+
"path": os.fspath(test_nested_folder_one_path),
397397
"type_": "folder",
398-
"id_": os.fspath(test_z_nested_folder_one_path),
398+
"id_": os.fspath(test_nested_folder_one_path),
399399
"children": [
400400
{
401401
"name": "test_bottom_folder.py",
@@ -412,11 +412,11 @@
412412
),
413413
"type_": "test",
414414
"id_": get_absolute_test_id(
415-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
415+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
416416
test_bottom_folder_path,
417417
),
418418
"runID": get_absolute_test_id(
419-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
419+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
420420
test_bottom_folder_path,
421421
),
422422
},
@@ -429,11 +429,11 @@
429429
),
430430
"type_": "test",
431431
"id_": get_absolute_test_id(
432-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
432+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
433433
test_bottom_folder_path,
434434
),
435435
"runID": get_absolute_test_id(
436-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
436+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
437437
test_bottom_folder_path,
438438
),
439439
},

pythonFiles/tests/pytestadapter/expected_execution_test_output.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@
308308
# └── test_top_folder.py
309309
# └── test_top_function_t: success
310310
# └── test_top_function_f: failure
311-
# └── z_nested_folder_one
311+
# └── nested_folder_one
312312
# └── test_bottom_folder.py
313313
# └── test_bottom_function_t: success
314314
# └── test_bottom_function_f: failure
@@ -318,7 +318,7 @@
318318
dual_level_nested_folder_bottom_path = (
319319
TEST_DATA_PATH
320320
/ "dual_level_nested_folder"
321-
/ "z_nested_folder_one"
321+
/ "nested_folder_one"
322322
/ "test_bottom_folder.py"
323323
)
324324
dual_level_nested_folder_execution_expected_output = {
@@ -345,11 +345,11 @@
345345
"subtest": None,
346346
},
347347
get_absolute_test_id(
348-
"z_nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
348+
"nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
349349
dual_level_nested_folder_bottom_path,
350350
): {
351351
"test": get_absolute_test_id(
352-
"z_nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
352+
"nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
353353
dual_level_nested_folder_bottom_path,
354354
),
355355
"outcome": "success",
@@ -358,11 +358,11 @@
358358
"subtest": None,
359359
},
360360
get_absolute_test_id(
361-
"z_nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
361+
"nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
362362
dual_level_nested_folder_bottom_path,
363363
): {
364364
"test": get_absolute_test_id(
365-
"z_nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
365+
"nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
366366
dual_level_nested_folder_bottom_path,
367367
),
368368
"outcome": "failure",
@@ -479,7 +479,7 @@
479479
dual_level_nested_folder_bottom_path = (
480480
TEST_DATA_PATH
481481
/ "dual_level_nested_folder"
482-
/ "z_nested_folder_one"
482+
/ "nested_folder_one"
483483
/ "test_bottom_folder.py"
484484
)
485485
unittest_folder_add_path = TEST_DATA_PATH / "unittest_folder" / "test_add.py"

pythonFiles/tests/pytestadapter/test_discovery.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33
import os
4+
import pathlib
45
import shutil
6+
import sys
57
from typing import Any, Dict, List, Optional
68

79
import pytest
810

11+
script_dir = pathlib.Path(__file__).parent.parent
12+
sys.path.append(os.fspath(script_dir))
13+
14+
from tests.tree_comparison_helper import is_same_tree
15+
916
from . import expected_discovery_test_output
1017
from .helpers import TEST_DATA_PATH, runner, runner_with_cwd
1118

@@ -195,7 +202,7 @@ def test_pytest_collect(file, expected_const):
195202
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
196203
assert actual_item.get("status") == "success"
197204
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH)
198-
assert actual_item.get("tests") == expected_const
205+
assert is_same_tree(actual_item.get("tests"), expected_const)
199206

200207

201208
def test_pytest_root_dir():
@@ -219,9 +226,9 @@ def test_pytest_root_dir():
219226
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
220227
assert actual_item.get("status") == "success"
221228
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH / "root")
222-
assert (
223-
actual_item.get("tests")
224-
== expected_discovery_test_output.root_with_config_expected_output
229+
assert is_same_tree(
230+
actual_item.get("tests"),
231+
expected_discovery_test_output.root_with_config_expected_output,
225232
)
226233

227234

@@ -245,7 +252,7 @@ def test_pytest_config_file():
245252
assert all(item in actual_item.keys() for item in ("status", "cwd", "error"))
246253
assert actual_item.get("status") == "success"
247254
assert actual_item.get("cwd") == os.fspath(TEST_DATA_PATH / "root")
248-
assert (
249-
actual_item.get("tests")
250-
== expected_discovery_test_output.root_with_config_expected_output
255+
assert is_same_tree(
256+
actual_item.get("tests"),
257+
expected_discovery_test_output.root_with_config_expected_output,
251258
)

pythonFiles/tests/pytestadapter/test_execution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ def test_bad_id_error_execution():
193193
[
194194
"dual_level_nested_folder/test_top_folder.py::test_top_function_t",
195195
"dual_level_nested_folder/test_top_folder.py::test_top_function_f",
196-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
197-
"dual_level_nested_folder/z_nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
196+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_t",
197+
"dual_level_nested_folder/nested_folder_one/test_bottom_folder.py::test_bottom_function_f",
198198
],
199199
expected_execution_test_output.dual_level_nested_folder_execution_expected_output,
200200
),

pythonFiles/tests/unittestadapter/helpers.py renamed to pythonFiles/tests/tree_comparison_helper.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
import pathlib
5-
6-
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
7-
84

95
def is_same_tree(tree1, tree2) -> bool:
106
"""Helper function to test if two test trees are the same.
@@ -17,8 +13,9 @@ def is_same_tree(tree1, tree2) -> bool:
1713

1814
# Compare child test nodes if they exist, otherwise compare test items.
1915
if "children" in tree1 and "children" in tree2:
20-
children1 = tree1["children"]
21-
children2 = tree2["children"]
16+
# sort children by path before comparing since order doesn't matter of children
17+
children1 = sorted(tree1["children"], key=lambda x: x["path"])
18+
children2 = sorted(tree2["children"], key=lambda x: x["path"])
2219

2320
# Compare test nodes.
2421
if len(children1) != len(children2):

pythonFiles/tests/unittestadapter/expected_discovery_test_output.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import os
55
from unittestadapter.pvsc_utils import TestNodeTypeEnum
6-
from .helpers import TEST_DATA_PATH
76
import pathlib
87

8+
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
9+
10+
911
skip_unittest_folder_discovery_output = {
1012
"path": os.fspath(TEST_DATA_PATH / "unittest_skip"),
1113
"name": "unittest_skip",

pythonFiles/tests/unittestadapter/test_discovery.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33

44
import os
55
import pathlib
6+
import sys
67
from typing import List
78

89
import pytest
910
from unittestadapter.discovery import discover_tests
1011
from unittestadapter.pvsc_utils import TestNodeTypeEnum, parse_unittest_args
1112

13+
script_dir = pathlib.Path(__file__).parent.parent
14+
sys.path.append(os.fspath(script_dir))
15+
16+
1217
from . import expected_discovery_test_output
13-
from .helpers import TEST_DATA_PATH, is_same_tree
18+
from tests.tree_comparison_helper import is_same_tree
19+
20+
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
1421

1522

1623
@pytest.mark.parametrize(

pythonFiles/tests/unittestadapter/test_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import pathlib
6+
import sys
67
import unittest
78

89
import pytest
@@ -15,7 +16,13 @@
1516
get_test_case,
1617
)
1718

18-
from .helpers import TEST_DATA_PATH, is_same_tree
19+
script_dir = pathlib.Path(__file__).parent.parent
20+
sys.path.append(os.fspath(script_dir))
21+
22+
from tests.tree_comparison_helper import is_same_tree
23+
24+
25+
TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data"
1926

2027

2128
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)