Repository navigation
Keep states defined in test_state tests out of later tests - #7963
benedikt-bartscher wants to merge 1 commit into
Conversation
Several tests in test_state.py define states that register themselves in the class-level dependency maps of module-level states, and clean those maps up at the end of the test body, so a failing test leaves its entries behind. test_computed_var_mutability does not clean up at all and leaves states with an invalid dependency registered, which fails every later app compile in the session. The redis config tests leave a redis config in the root registration context. forked_registration_context now also restores the dependency tracking of the states registered before the fork, and these tests use it instead of cleaning up by hand.
[Low impact] This test-only change appears safe to merge. SummaryThis test-only change keeps temporary states and Redis settings out of later tests.
Reviews (1) · Last reviewed commit: "Keep states defined in test_state tests ..." · Reviewed by Greptile |
Merging this PR will degrade performance by 6.06%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Summary
Some tests in
test_state.pyleave registrations behind that break later tests, depending on the order the tests run in. CI runs the unit tests in default order, wheretest_app.pycomes beforetest_state.py, so it doesn't hit this. Running a selection with-k,--lf, or the files in another order does.test_computed_var_mutabilitydefines states with a deliberately invaliddeps=["hi"]and never removes them. Every laterAppcompile then fails dependency validation.pytest tests/units/test_state.py tests/units/test_app.py -p no:randomlyfails 17test_apptests onmain.test_composite_var_dep_tracks_fields_in_every_stateregister their local states in the class-level dependency maps ofStateand other module-level states. They remove those entries at the end of the test body, so a test that fails leaves them behind, and the next test that dirties one of those vars looks up a substate that no longer exists. Under random order,test_state.pyalone fails 7 of 8 seeds (--randomly-seed=1..8).reload_config()with aredis://localhostconfig, which stays on the root registration context. Later tests then create a redis state manager and try to connect.forked_registration_contextalready gives a test its own copy of the registrations and the config. It now also snapshots the dependency tracking (_var_dependencies,_potentially_dirty_states,_always_dirty_substates) of the states registered before the fork, and restores it on teardown. That covers the entries a state defined in the fork adds to classes that outlive it. The tests above use the fixture, and the manual cleanup at the end of their bodies is gone, so teardown runs even when an assertion fails.Test-only change, no news fragment.
Test plan
pytest tests/units/test_state.py tests/units/test_app.py -p no:randomly: 17 failed before, 670 passed afterpytest tests/units/test_state.py --randomly-seed=Nfor N = 1..10: 336 passed for every seed (before: failures on 7 of seeds 1..8)pytest tests/units/test_state.py tests/units/test_app.py --randomly-seed=Nfor N = 1..4: 670 passeduv run pytest tests/units: 16458 passedruff check,ruff format --check,pyright🤖 Generated with Claude Code