Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix least duration algo with random ordering #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- The `least_duration` algorithm now splits deterministically regardless of the starting test order, even in cases where identically named test cases exist.

## [0.10.0] - 2024-10-16
### Added
Expand Down
2 changes: 1 addition & 1 deletion src/pytest_split/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __call__(

# Sort by name to ensure it's always the same order
items_with_durations_indexed = sorted(
items_with_durations_indexed, key=lambda tup: str(tup[0])
items_with_durations_indexed, key=lambda tup: tup[0].nodeid
)

# sort in ascending order
Expand Down
51 changes: 51 additions & 0 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
from collections import namedtuple
from dataclasses import dataclass
from typing import TYPE_CHECKING

import pytest
Expand All @@ -17,6 +18,17 @@
item = namedtuple("item", "nodeid") # noqa: PYI024


@dataclass
class DummyPytestItem:
name: str
nodeid: str

def __repr__(self) -> str:
return "<{} {}>".format(self.__class__.__name__, getattr(self, "name", None))

def __eq__(self, value: object) -> bool:
self.nodeid == value.nodeid

class TestAlgorithms:
@pytest.mark.parametrize("algo_name", Algorithms.names())
def test__split_test(self, algo_name):
Expand Down Expand Up @@ -140,6 +152,45 @@ def test__algorithms_members_derived_correctly(self):
for a in Algorithms.names():
assert issubclass(Algorithms[a].value.__class__, AlgorithmBase)

def test__split_tests_correctly_same_names_with_real_items(self, tmp_path):
"""Test that least_duration algorithm works correctly with real pytest Items
that have same names but different paths."""
items = [
DummyPytestItem(
name="test_something_a", nodeid="dir_a/test.py::test_something_a"
),
DummyPytestItem(
name="test_something_a", nodeid="dir_b/test.py::test_something_a"
),
DummyPytestItem(
name="test_something_b", nodeid="dir_a/test.py::test_something_b"
),
DummyPytestItem(
name="test_something_b", nodeid="dir_b/test.py::test_something_b"
),
]

first_randomization = (0, 1, 2, 3)
second_randomization = (1, 0, 3, 2)

expected_groups = [[items[0], items[1]], [items[2], items[3]]]

durations = {item.nodeid: 1 for item in items}

algo = Algorithms["least_duration"].value
split_number = 2

for randomization in (first_randomization, second_randomization):
randomized_items = [items[index] for index in randomization]
splits = algo(
splits=split_number, items=randomized_items, durations=durations
)

for index, group in enumerate(splits):
assert (
sorted(group.selected, key=lambda item: item.nodeid)
== expected_groups[index]
)

class MyAlgorithm(AlgorithmBase):
def __call__(self, a, b, c):
Expand Down