Skip to content

Commit

Permalink
plugin: sort tests by topology name
Browse files Browse the repository at this point in the history
This is needed in order to allow one-time topology setup and teardown.

If the tests would not be sorted by topology, we would have to run
topology setup and teardown for each test, which has its use cases
but in most majority of cases you want to setup environment for
given topology only once and then run all tests for this topology
in order to speed things up.

We will provide hooks to do both - one-time setup and per-test setup.
  • Loading branch information
pbrezina committed Jan 29, 2024
1 parent 9e5b5a4 commit 5753aa1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pytest_mh/_private/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ def pytest_collection_modifyitems(self, config: pytest.Config, items: list[pytes
:meta private:
"""

selected = []
deselected = []
selected: list[pytest.Item] = []
deselected: list[pytest.Item] = []
mapping: dict[str, list[pytest.Item]] = {}

for item in items:
data = MultihostItemData(self.multihost, item.stash[MarkStashKey]) if self.multihost else None
Expand All @@ -229,8 +230,16 @@ def pytest_collection_modifyitems(self, config: pytest.Config, items: list[pytes
deselected.append(item)
continue

selected.append(item)
# Map test items by topology name so we can sort them later
if data is None or data.topology_mark is None:
mapping.setdefault("", []).append(item)
else:
mapping.setdefault(data.topology_mark.name, []).append(item)

# Sort test by topology name
selected = sum([y for _, y in sorted(mapping.items())], [])

# Yield result to pytest
config.hook.pytest_deselected(items=deselected)
items[:] = selected

Expand Down

0 comments on commit 5753aa1

Please sign in to comment.