Skip to content

5/5 fix: stabilise duration_based_chunks across collection orders#124

Open
vokracko wants to merge 5 commits intojerry-git:masterfrom
vokracko:fix/duration-based-chunks-stable-membership
Open

5/5 fix: stabilise duration_based_chunks across collection orders#124
vokracko wants to merge 5 commits intojerry-git:masterfrom
vokracko:fix/duration-based-chunks-stable-membership

Conversation

@vokracko
Copy link
Copy Markdown

Fifth and final PR in the stack. This is the user-visible fix: duration_based_chunks group memberships are now stable across pytest collection orders. Closes #25.

Motivation. duration_based_chunks previously consumed pytest's collected order directly, so a parametrize fed a hash-randomised iterable (set, frozenset, dict.keys()) under PYTHONHASHSEED=random could move tests between groups across runs — churning committed .test_durations files and CI group manifests with no real code change. 0.8.0 fixed this for least_duration (sort-by-name) but the same fix was never extended to duration_based_chunks. The previous four PRs were preparation; this one swaps in canonical (nodeid-sorted) ordering at the plugin level so membership becomes a pure function of the (nodeid, duration) multiset and the requested split count.

What changes. The plugin sorts the durations dict by nodeid before invoking the algorithm. LeastDurationAlgorithm drops its now-redundant internal nodeid sort. DurationBasedChunksAlgorithm is unchanged — it walks the dict in iteration order, which is now canonical thanks to the plugin step.

Within-group execution order is unchanged — pytest still runs each group's tests in collection order, courtesy of the helper added in 4/5. The user-visible change is a one-time shift in duration_based_chunks membership distribution; the .test_durations file format is unchanged. README's algorithms table updated.

Stack.

The previous setup relied on pytest-cov (``--cov pytest_split
--cov tests`` in addopts) which has a long-standing limitation when
measuring a pytest plugin: the plugin imports happen during pytest's
plugin discovery, *before* pytest-cov starts tracing. Anything that
runs at import time (class bodies, decorators, type aliases, enum
definitions) is reported as uncovered even though it obviously
executed - the plugin wouldn't load otherwise. The
``CoverageWarning: Module pytest_split was previously imported, but
not measured`` made that explicit.

The cookiecutter template the project was generated from worked
around this by adding ``--cov tests`` to the addopts, inflating the
denominator with 100%-covered test files so the average reached 90%.
That pulled the number up but stopped reporting the actual source
coverage.

Switch to ``coverage run -m pytest`` followed by ``coverage report``.
Running ``coverage`` from the outside means tracing starts before
Python imports anything at all, so plugin import-time code is
counted. Real source coverage jumps from ~74% to 99%; threshold goes
from 90 to 95 to reflect that.
Tests previously asserted each TestGroup field separately
(`first.selected`, `first.deselected`, `first.duration`). Consolidate
to a single equality check against an expected TestGroup, with
concrete values for every field rather than only ``selected``.
Tighten the algorithm contract so each one receives a single data
argument: a dict mapping pytest items to their durations. Promote the
helper that builds the dict to a public name (compute_durations) and
inline the two helpers that only it used.

The plugin builds the dict once and passes it to the algorithm.
Algorithm bodies otherwise consume the same (item, duration) stream
they did before, so behaviour is unchanged.

Sets up follow-ups that separate group membership from within-group
ordering.
Algorithms now own only group membership; the plugin owns the order
in which selected/deselected items are emitted to pytest.

LeastDurationAlgorithm drops the original_index bookkeeping. Its body
collapses to a name + duration sort followed by the heap-based
balanced assignment, with selected ending up in heap-pop order.

The plugin's collection hook then rebuilds the chosen group's
selected and deselected lists in pytest's collection order, keyed on
nodeid. That's now the single source of truth for preserving
collection order, so subsequent algorithm changes don't have to worry
about it.

End-to-end behaviour is unchanged: pytest still executes each group's
tests in the order it collected them.
Group membership for duration_based_chunks previously depended on the
order pytest happened to collect tests, so a parametrize fed a
hash-randomised iterable (set, frozenset, dict.keys()) under
PYTHONHASHSEED=random could move tests between groups across runs,
producing churn in committed durations files and CI group manifests
even when nothing changed.

The plugin now sorts the durations dict by nodeid before invoking the
algorithm. Membership for both algorithms is now a pure function of
the (nodeid, duration) multiset and the requested split count.

LeastDurationAlgorithm drops its now-redundant nodeid pre-sort
(stable sort by duration desc on a canonically-ordered dict has the
same effect). DurationBasedChunksAlgorithm walks the canonical-
ordered dict unchanged.

Within-group execution order is still pytest's collection order, so
the only user-visible change is that duration_based_chunks membership
distribution shifts once. .test_durations file format is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Splits invalid when collection order not deterministic

1 participant