From 4ebb9b32e73b63c94c6c9d80e6f7c6f8bc6f3a6b Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Wed, 7 Jun 2023 22:00:53 +0200 Subject: [PATCH] test/verify: sort tests by nondestructiveness When running test/verify tests via pytest, sort them by their nondestructiveness. This has two advantages: - the tests that can use the global machine are placed beside each other, preventing the machine from being killed in between - we sort nondestructive tests to the start under the assumption that they're usually smaller. This allows us to run a lot of tests at the start to quickly catch issues. --- test/verify/conftest.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/verify/conftest.py b/test/verify/conftest.py index b81f24851243..0b8888372c09 100644 --- a/test/verify/conftest.py +++ b/test/verify/conftest.py @@ -1,9 +1,10 @@ import importlib.machinery import importlib.util from pathlib import Path -from typing import Optional +from typing import List, Optional import pytest +import testlib @pytest.hookimpl @@ -24,3 +25,17 @@ def pytest_collect_file(file_path: Path, parent: pytest.Collector) -> Optional[p return collector return None + + +@pytest.hookimpl +def pytest_collection_modifyitems(session: pytest.Session, items: List[pytest.Item]) -> None: + """Sorts the tests to place all non-destructive tests together""" + assert isinstance(items, list) + + def is_nondestructive(item: pytest.Item) -> bool: + assert isinstance(item, pytest.Function) + assert isinstance(item.parent, pytest.Class | pytest.Module) + return testlib.get_decorator(item.obj, item.parent.obj, "nondestructive", False) + + # put the destructive tests last under the assumption that they're slower + items.sort(key=is_nondestructive, reverse=True)