From cc32370d49600e0d6d95c9185d449c45760d9e5d Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Sun, 28 Dec 2025 07:25:18 +0000 Subject: [PATCH] perf: speed up test suite with parallel execution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Enable pytest-xdist with -n auto for parallel test execution - Consolidate migrated_db fixture in conftest.py (was duplicated in 4 files) - Remove local fixture definitions and unused imports from test files Test execution: ~24s -> ~5s (~5x speedup) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- pyproject.toml | 2 +- tests/conftest.py | 11 +++++++++++ tests/test_migration_event_tables.py | 12 ------------ tests/test_migration_reference_tables.py | 12 ------------ tests/test_repositories.py | 10 ---------- tests/test_seeds.py | 12 ------------ 6 files changed, 12 insertions(+), 47 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5c32707..726f570 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -51,4 +51,4 @@ pythonpath = ["src"] python_files = "test_*.py" python_classes = "Test*" python_functions = "test_*" -addopts = "--durations=20" +addopts = "--durations=20 -n auto" diff --git a/tests/conftest.py b/tests/conftest.py index e75eb29..81ccbfa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,6 +6,17 @@ import tempfile import pytest +from animaltrack.db import get_db +from animaltrack.migrations import run_migrations + + +@pytest.fixture +def migrated_db(tmp_path): + """Create a database with migrations applied.""" + db_path = str(tmp_path / "test.db") + run_migrations(db_path, "migrations", verbose=False) + return get_db(db_path) + @pytest.fixture def temp_migrations_dir(tmp_path): diff --git a/tests/test_migration_event_tables.py b/tests/test_migration_event_tables.py index d378471..b77bb3c 100644 --- a/tests/test_migration_event_tables.py +++ b/tests/test_migration_event_tables.py @@ -6,18 +6,6 @@ import json import apsw import pytest -from animaltrack.db import get_db -from animaltrack.migrations import run_migrations - - -@pytest.fixture -def migrated_db(tmp_path): - """Create a database with migrations applied.""" - db_path = str(tmp_path / "test.db") - migrations_dir = "migrations" - run_migrations(db_path, migrations_dir, verbose=False) - return get_db(db_path) - class TestMigrationCreatesAllTables: """Tests that migration creates all event tables.""" diff --git a/tests/test_migration_reference_tables.py b/tests/test_migration_reference_tables.py index ae70c25..965349c 100644 --- a/tests/test_migration_reference_tables.py +++ b/tests/test_migration_reference_tables.py @@ -4,18 +4,6 @@ import apsw import pytest -from animaltrack.db import get_db -from animaltrack.migrations import run_migrations - - -@pytest.fixture -def migrated_db(tmp_path): - """Create a database with migrations applied.""" - db_path = str(tmp_path / "test.db") - migrations_dir = "migrations" - run_migrations(db_path, migrations_dir, verbose=False) - return get_db(db_path) - class TestMigrationCreatesAllTables: """Tests that migration creates all reference tables.""" diff --git a/tests/test_repositories.py b/tests/test_repositories.py index 2919d82..d9b3a05 100644 --- a/tests/test_repositories.py +++ b/tests/test_repositories.py @@ -5,9 +5,7 @@ import time import pytest -from animaltrack.db import get_db from animaltrack.id_gen import generate_id -from animaltrack.migrations import run_migrations from animaltrack.models.reference import ( FeedType, Location, @@ -26,14 +24,6 @@ from animaltrack.repositories import ( ) -@pytest.fixture -def migrated_db(tmp_path): - """Create a database with migrations applied.""" - db_path = str(tmp_path / "test.db") - run_migrations(db_path, "migrations", verbose=False) - return get_db(db_path) - - @pytest.fixture def now_utc(): """Current time in milliseconds since epoch.""" diff --git a/tests/test_seeds.py b/tests/test_seeds.py index e2ba843..4ac9d19 100644 --- a/tests/test_seeds.py +++ b/tests/test_seeds.py @@ -1,10 +1,6 @@ # ABOUTME: Tests for the reference data seeder. # ABOUTME: Validates that seeding populates correct data and is idempotent. -import pytest - -from animaltrack.db import get_db -from animaltrack.migrations import run_migrations from animaltrack.models.reference import ProductUnit, UserRole from animaltrack.repositories import ( FeedTypeRepository, @@ -16,14 +12,6 @@ from animaltrack.repositories import ( from animaltrack.seeds import run_seeds -@pytest.fixture -def migrated_db(tmp_path): - """Create a database with migrations applied.""" - db_path = str(tmp_path / "test.db") - run_migrations(db_path, "migrations", verbose=False) - return get_db(db_path) - - class TestSeedCounts: """Tests that seeding creates the correct number of records."""