From fc9c5ea3c1ead0328778a53f123baa56bb3d6000 Mon Sep 17 00:00:00 2001 From: Jens Tirsvad Nielsen Date: Thu, 15 Oct 2026 00:43:54 +0800 Subject: [PATCH] Fix a flaky unit test that created a real Tk root test_main_returns_one_when_word_list_is_missing_with_level called app.main without a fake root, so a real tk.Tk() was created in a unit test and failed about 1 in 6 full runs. An autouse fixture now patches app.tk.Tk with the module's fake root. Review record RC-049 (Go). Task: MIL-009#9 --- tests/unit/test_app.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_app.py b/tests/unit/test_app.py index 64bc186..785a2dc 100644 --- a/tests/unit/test_app.py +++ b/tests/unit/test_app.py @@ -92,6 +92,12 @@ def _first(cards: Sequence[Card]) -> Card: return cards[0] +@pytest.fixture(autouse=True) +def fake_tk_root(monkeypatch: pytest.MonkeyPatch) -> None: + """Keep every test in this module away from a real Tk root.""" + monkeypatch.setattr(app.tk, "Tk", _FakeRoot) + + @pytest.fixture def fake_views(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(app, "LevelView", _FakeLevelView) @@ -280,7 +286,6 @@ def test_create_application_draws_first_card_from_bundled_list( bundled.parent.mkdir() bundled.write_text("ms;da\nsaya;jeg\n", encoding="utf-8") data = tmp_path / "data" - monkeypatch.setattr(app.tk, "Tk", _FakeRoot) monkeypatch.setattr(app, "DEFAULT_DATA_DIR", data) monkeypatch.setattr(app, "BUNDLED_VOCABULARY_PATH", bundled) application = app.create_application(None, False, Level(None))