diff --git a/CHANGELOG.md b/CHANGELOG.md index ecca0f1..f6e0e1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - added official support for Python 3.15 ### Fixes +- class-level relative ordering resolves nested classes and includes their nested tests - relative ordering resolves whole-test labels when parameter IDs contain brackets or node separators (see [#272](https://github.com/pytest-dev/pytest-order/issues/272)) - class-scoped ordering handles parameter IDs containing `::`, such as IPv6 addresses diff --git a/src/pytest_order/sorter.py b/src/pytest_order/sorter.py index f01f39a..4705e6d 100644 --- a/src/pytest_order/sorter.py +++ b/src/pytest_order/sorter.py @@ -194,7 +194,11 @@ def items_from_label(self, label: str, item: Item, is_cls_mark: bool) -> list[It ] else: continue - if is_cls_mark and id_start.count("::") == 2: + if ( + is_cls_mark + and "::" not in label + and self.node_ids[node_id].item.cls + ): continue if item_id.startswith(id_start): items.append(self.node_ids[node_id]) @@ -206,13 +210,12 @@ def items_from_class_label(self, label: str, item: Item) -> list[Item]: item_id = item.node_id label_len = len(label) for candidate in self.node_ids.values(): - node_id = candidate.node_id_without_parameters - if node_id.count("::") == 2: - cls_index = node_id.rindex("::") - if node_id[:cls_index].endswith(label): - id_start = node_id[: cls_index - label_len] + for node in candidate.item.listchain(): + if isinstance(node, Class) and node.nodeid.endswith(label): + id_start = node.nodeid[:-label_len] if item_id.startswith(id_start): items.append(candidate) + break return items def handle_before_or_after_mark( diff --git a/tests/test_relative_ordering.py b/tests/test_relative_ordering.py index 7c2dcb5..ec901b2 100644 --- a/tests/test_relative_ordering.py +++ b/tests/test_relative_ordering.py @@ -1,4 +1,4 @@ -from textwrap import dedent +from textwrap import dedent, indent import pytest @@ -701,3 +701,114 @@ def test_source(self, value): "TestSource::test_source[nested[::id]]", "TestAfter::test_after", ] + + +@pytest.mark.parametrize("depth", [1, 2]) +@pytest.mark.parametrize("direction", ["before", "after"]) +def test_relative_nested_classes(item_names_for, depth, direction): + anchor = """class TestAnchor: + @pytest.mark.parametrize("value", [1, 2], ids=["nested[::id]", "path/::id"]) + def test_anchor(self, value): + pass +""" + dependent = f"""@pytest.mark.order({direction}="TestAnchor") +class TestDependent: + def test_dependent(self): + pass +""" + tests = anchor + dependent if direction == "before" else dependent + anchor + for level in range(depth): + tests = f"class TestOuter{level}:\n" + indent(tests, " ") + expected = [ + "TestAnchor::test_anchor[nested[::id]]", + "TestAnchor::test_anchor[path/::id]", + ] + if direction == "before": + expected.insert(0, "TestDependent::test_dependent") + else: + expected.append("TestDependent::test_dependent") + assert item_names_for("import pytest\n" + tests) == expected + + +@pytest.mark.parametrize("direction", ["before", "after"]) +def test_relative_class_includes_nested_tests(item_names_for, direction): + anchor = """class TestAnchor: + def test_direct(self): + pass + + class TestNested: + def test_nested(self): + pass +""" + dependent = f"""@pytest.mark.order({direction}="TestAnchor") +class TestDependent: + def test_dependent(self): + pass +""" + tests = anchor + dependent if direction == "before" else dependent + anchor + expected = ["TestAnchor::test_direct", "TestNested::test_nested"] + if direction == "before": + expected.insert(0, "TestDependent::test_dependent") + else: + expected.append("TestDependent::test_dependent") + assert item_names_for("import pytest\n" + tests) == expected + + +def test_class_marker_references_nested_method(item_names_for): + tests = """ + import pytest + + class TestOuter: + @pytest.mark.order(after="TestAnchor::test_anchor") + class TestDependent: + def test_dependent(self): + pass + + class TestAnchor: + def test_anchor(self): + pass + """ + assert item_names_for(tests) == [ + "TestAnchor::test_anchor", + "TestDependent::test_dependent", + ] + + +def test_nested_class_marker_does_not_reference_bare_method(item_names_for): + tests = """ + import pytest + + class TestOuter: + @pytest.mark.order(after="test_anchor") + class TestDependent: + def test_dependent(self): + pass + + def test_anchor(self): + pass + """ + assert item_names_for(tests) == [ + "TestDependent::test_dependent", + "TestDependent::test_anchor", + ] + + +def test_relative_class_marker_inherited_by_nested_class(item_names_for): + tests = """ + import pytest + + @pytest.mark.order(after="TestAnchor") + class TestDependent: + class TestNestedDependent: + def test_dependent(self): + pass + + class TestAnchor: + class TestNestedAnchor: + def test_anchor(self): + pass + """ + assert item_names_for(tests) == [ + "TestNestedAnchor::test_anchor", + "TestNestedDependent::test_dependent", + ]