From fcab6ec7666dc3dd9ee04e8450150bdc652a365a Mon Sep 17 00:00:00 2001 From: Rbanh Date: Sun, 23 Feb 2025 12:25:28 -0500 Subject: [PATCH] Fix food spawn test and optimize test execution --- pytest.ini | 6 +++++- requirements.txt | 3 ++- src/food.py | 18 ++++++++---------- tests/conftest.py | 9 +++++---- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/pytest.ini b/pytest.ini index 3f7a136..352e605 100644 --- a/pytest.ini +++ b/pytest.ini @@ -3,4 +3,8 @@ testpaths = tests python_files = test_*.py python_classes = Test* python_functions = test_* -addopts = -v --tb=short \ No newline at end of file +addopts = -v --tb=short -n auto +markers = + slow: marks tests as slow (deselect with '-m "not slow"') + integration: marks tests as integration tests + unit: marks tests as unit tests \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index ca289e4..8db134c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ pygame==2.5.2 numpy==1.24.3 black==23.12.1 # for code formatting -pytest==7.4.3 # for testing \ No newline at end of file +pytest==7.4.3 # for testing +pytest-xdist==3.5.0 # for parallel test execution \ No newline at end of file diff --git a/src/food.py b/src/food.py index 78156fd..c1aa3e6 100644 --- a/src/food.py +++ b/src/food.py @@ -17,22 +17,20 @@ class Food: height: Game area height occupied_positions: List of positions (typically snake body positions) where food shouldn't spawn """ - # Calculate valid grid positions - valid_x = range(0, width, self.block_size) - valid_y = range(0, height, self.block_size) - - # Create list of all possible positions - all_positions = [ - (x, y) for x in valid_x for y in valid_y + # Calculate all valid grid positions + valid_positions = [ + (x, y) + for x in range(0, width, self.block_size) + for y in range(0, height, self.block_size) if (x, y) not in occupied_positions ] - if not all_positions: + if not valid_positions: # No valid positions (snake fills screen) - game should be won return - # Choose random position - self.position = random.choice(all_positions) + # Choose random position from valid positions + self.position = random.choice(valid_positions) def draw(self, screen: pygame.Surface) -> None: """Draw the food on the screen""" diff --git a/tests/conftest.py b/tests/conftest.py index 6c0f708..93e2e90 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,11 +6,12 @@ import os # Add src directory to Python path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) -@pytest.fixture(autouse=True) -def pygame_init(): - """Initialize pygame for all tests""" +def pytest_configure(config): + """Initialize pygame once before any tests run""" pygame.init() - yield + +def pytest_unconfigure(config): + """Clean up pygame after all tests are done""" pygame.quit() @pytest.fixture