From 6287005ad1ac1ad5d069727c798d8f4da6148eca Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 8 Jun 2026 00:47:03 -0400 Subject: [PATCH] test(collection_order): add red tests for opt-in sort_items_by_order --- tests/test_pytest_collection_order.py | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/test_pytest_collection_order.py diff --git a/tests/test_pytest_collection_order.py b/tests/test_pytest_collection_order.py new file mode 100644 index 00000000..dbb3deaa --- /dev/null +++ b/tests/test_pytest_collection_order.py @@ -0,0 +1,37 @@ +from pathlib import Path +import pytest + +def test_no_op_without_registry(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.chdir(tmp_path) + (tmp_path / "test_zz.py").write_text( + "def test_b(): pass\ndef test_a(): pass\n", encoding="utf-8" + ) + from pytest_collection_order import sort_items_by_order + items = [] + result = sort_items_by_order(items, registry_path=tmp_path / "reg.toml") + assert result == items + +def test_sorts_by_order_index(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.chdir(tmp_path) + (tmp_path / "test_zz.py").write_text( + "def test_b(): pass\ndef test_a(): pass\n", encoding="utf-8" + ) + reg = tmp_path / "reg.toml" + reg.write_text( + "[[files.test_zz.test_order]]\n" + "test_id = 'test_zz::test_b'\n" + "order = 1\n" + "[[files.test_zz.test_order]]\n" + "test_id = 'test_zz::test_a'\n" + "order = 2\n", + encoding="utf-8", + ) + class _Item: + def __init__(self, nodeid: str) -> None: + self.nodeid = nodeid + def __repr__(self) -> str: + return f"_Item({self.nodeid!r})" + items = [_Item("test_zz::test_b"), _Item("test_zz::test_a")] + from pytest_collection_order import sort_items_by_order + result = sort_items_by_order(items, registry_path=reg) + assert [i.nodeid for i in result] == ["test_zz::test_b", "test_zz::test_a"]