diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..620e26c --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,16 @@ +"""Shared pytest configuration for the snake game tests.""" + +from pathlib import Path + +import pytest + + +@pytest.fixture(autouse=True) +def isolated_working_directory(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """Run every test in an empty temporary working directory. + + The high score file has a relative default path (`data.txt`). Without this + fixture, a test that uses the default would read and write the real file of + the repository or of the directory pytest was started from. + """ + monkeypatch.chdir(tmp_path) diff --git a/tests/test_main.py b/tests/test_main.py index 60aaed6..0c11448 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,5 +1,7 @@ """Tests of the main flow, with fakes instead of a real screen.""" +from pathlib import Path + import pytest from fakes import ( @@ -131,6 +133,16 @@ def test_main_sets_up_the_screen_turns_off_drawing_and_binds_the_keys( assert sorted(screens[0].bindings) == sorted(ARROW_KEYS) +def test_main_creates_data_txt_in_the_working_directory_holding_0( + monkeypatch: pytest.MonkeyPatch, sleeps: list[float], tmp_path: Path +) -> None: + install_fake_turtle(monkeypatch) + + main() + + assert (tmp_path / constants.HIGH_SCORE_FILE).read_text(encoding="utf-8") == "0" + + def test_register_head_shape_builds_two_components_from_the_constants( monkeypatch: pytest.MonkeyPatch, ) -> None: