Fix food spawn test and optimize test performance

This commit is contained in:
Rbanh 2025-02-23 12:30:39 -05:00
parent fcab6ec766
commit afd17d257e
3 changed files with 13 additions and 8 deletions

View File

@ -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

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
}

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