From afd17d257e0ae5aa42bec2278d14fdea119a0a0a Mon Sep 17 00:00:00 2001 From: Rbanh Date: Sun, 23 Feb 2025 12:30:39 -0500 Subject: [PATCH] Fix food spawn test and optimize test performance --- pytest.ini | 2 +- tests/conftest.py | 7 +++++-- tests/test_food.py | 12 +++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pytest.ini b/pytest.ini index 352e605..b155b32 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,7 +3,7 @@ testpaths = tests python_files = test_*.py python_classes = Test* python_functions = test_* -addopts = -v --tb=short -n auto +addopts = -v --tb=short -n 4 # Limit to 4 parallel workers markers = slow: marks tests as slow (deselect with '-m "not slow"') integration: marks tests as integration tests diff --git a/tests/conftest.py b/tests/conftest.py index 93e2e90..6ff487f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,9 @@ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..') def pytest_configure(config): """Initialize pygame once before any tests run""" + # Set SDL to use software rendering and disable audio to reduce resource usage + os.environ['SDL_VIDEODRIVER'] = 'dummy' + os.environ['SDL_AUDIODRIVER'] = 'dummy' pygame.init() def pytest_unconfigure(config): @@ -18,8 +21,8 @@ def pytest_unconfigure(config): def game_config(): """Basic game configuration""" return { - 'width': 800, - 'height': 600, + 'width': 400, # Smaller dimensions for testing + 'height': 300, 'block_size': 20, 'fps': 60 } diff --git a/tests/test_food.py b/tests/test_food.py index 4e7ed72..d46d173 100644 --- a/tests/test_food.py +++ b/tests/test_food.py @@ -44,15 +44,17 @@ def test_food_spawn_full_grid(): food = Food(20) width, height = 60, 60 # Small grid - # Create a snake that occupies most of the grid - snake_body = [(x, y) for x in range(0, 60, 20) for y in range(0, 40, 20)] - - # Leave one spot free at (40, 40) + # Create a snake that occupies all positions except (40, 40) + all_positions = [(x, y) for x in range(0, 60, 20) for y in range(0, 60, 20)] free_spot = (40, 40) + snake_body = [pos for pos in all_positions if pos != free_spot] + + # Verify our test setup is correct + assert len(snake_body) == len(all_positions) - 1 assert free_spot not in snake_body # Spawn food food.spawn(width, height, snake_body) - # Food should spawn in the only free spot + # Food must spawn in the only free spot assert food.position == free_spot \ No newline at end of file