Testing
Gyrinx uses pytest for testing with Django integration. Tests are organized by app and follow consistent patterns.
Running Tests
Local Testing
# Run all tests
pytest
# Run tests for specific app
pytest gyrinx/core/tests/
pytest gyrinx/content/tests/
# Run specific test file
pytest gyrinx/core/tests/test_models_core.py
# Run specific test function
pytest gyrinx/core/tests/test_models_core.py::test_list_creation
# Run with verbose output
pytest -v
# Run with coverage
pytest --cov=gyrinxDocker Testing
Test Organization
Directory Structure
Test Patterns
Database Tests
All tests that use the database must be marked with @pytest.mark.django_db:
View Tests
Use Django's test client for testing views:
Model Tests
Test model methods, validation, and relationships:
Test Configuration
Static Files
Tests are configured to use StaticFilesStorage instead of CompressedManifestStaticFilesStorage to avoid manifest issues during testing. This is handled in conftest.py.
Database
Tests use a separate test database that's created and destroyed for each test run.
Fixtures
Use fixtures for common test data:
Writing Good Tests
Test Naming
Use descriptive test names that explain what is being tested
Follow the pattern:
test_<what>_<condition>_<expected_result>
Test Structure
Follow the Arrange-Act-Assert pattern:
Test Coverage
Test happy paths and edge cases
Test model validation and constraints
Test view permissions and responses
Test form validation
Test complex business logic
Performance
Use
pytest-django's database optimization featuresAvoid unnecessary database hits in tests
Use factories or fixtures for test data creation
Integration with CI/CD
Tests are automatically run in GitHub Actions on every pull request and push to main. The test suite must pass before code can be merged.
Common Issues
Static Files
If tests fail with static file issues, ensure you're not trying to render templates that require collected static files, or run manage collectstatic --noinput before testing.
Database Constraints
When testing models with foreign key constraints, ensure all required related objects are created first.
History Tracking
When testing models with history tracking, be aware that history records are created automatically and may affect test assertions.
Last updated