Skip to content

Test infrastructure keeps producing fixture state-leak bugs #5079

Description

@chalfontchubby

(Posted by Rik; written by Claude.)

Pattern

unit_test.py runs every registered test against one shared PredBat/HA fixture (create_predbat()), and this keeps producing the same class of bug: a test mutates shared state, restores it incompletely or unconditionally-to-a-default instead of to what was actually there, and leaks into whichever test runs next.

Recent instances, all found by review rather than by the leak actually being caught:

Each was fixed individually as found. The pattern itself isn't tracked anywhere, and there's no mechanism that would catch the next one before a maintainer or reviewer spots it by hand.

Why it keeps happening

  • The shared-fixture-across-all-tests design means any test that mutates instance state is a landmine for every test that runs after it, and the blast radius depends on run order.
  • "Snapshot before mutating, restore what was actually there" (not = {} or a hardcoded default) is a convention, not something enforced - easy to get right for the field you're thinking about and miss another one a function touches incidentally.
  • Nothing currently detects a leak automatically; it surfaces as an unrelated test failing later, which is confusing to debug and easy to misattribute (see test_state_fingerprint idea already on Rik's backlog, aimed at exactly this).

Possible directions (not scoped, for discussion)

  • A generic snapshot/restore helper other test modules could reuse instead of each writing its own (a couple of ad-hoc versions of this already exist, e.g. in test_inverter.py).
  • A registry-level assertion that fixture state is unchanged after each test (or each module) - would catch a leak at its source test rather than downstream.
  • Isolating truly destructive tests (ones that call something like fetch_rates()/_apply_rates() that touch dozens of fields) onto a throwaway PredBat instance rather than the shared one, since restoring 24 fields by hand doesn't scale.

Filed to track the pattern; no fix attempted here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BOT_TRIAGEDHas been through the triage botINFRAAffects the predbat development infrastructure - not directly the end product. Not an end user fixenhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions