Fix food spawn test and optimize test performance

This commit is contained in:
Rbanh committed 2025-02-23 12:30:39 -05:00
1 parent fcab6ec766
commit afd17d257e
3 files changed
+13 -8

No files matched your search

+5 -2
View File
@@ -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
}
+7 -5
View File
@@ -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