series of tests added with built-in django test support

This commit is contained in:
pacnpal
2024-11-05 18:40:39 +00:00
parent ba226c861a
commit 2e8a725933
60 changed files with 2108 additions and 274 deletions

84
tests/README.md Normal file
View File

@@ -0,0 +1,84 @@
# ThrillWiki Test Suite
This directory contains the comprehensive test suite for ThrillWiki, including unit tests and integration tests for all major components of the system.
## Running Tests
To run the complete test suite with coverage reporting:
```bash
python tests/test_runner.py
```
This will:
1. Run all tests across all apps
2. Generate a coverage report in the terminal
3. Create a detailed HTML coverage report in `tests/coverage_html/`
## Viewing Coverage Reports
There are two ways to view the coverage reports:
1. Terminal Report: Shows a quick overview of test coverage directly in your terminal after running the tests.
2. HTML Report: A detailed, interactive report showing line-by-line coverage that can be accessed in two ways:
- Directly open `tests/coverage_html/index.html` in your browser
- Visit `http://localhost:8000/coverage/` when running the development server (only available in DEBUG mode)
The HTML report provides:
- Line-by-line coverage analysis
- Branch coverage information
- Missing lines highlighting
- Interactive file browser
- Detailed statistics per module
## Test Structure
The test suite is organized by app, with each app having its own test file:
- `parks/tests.py`: Tests for park-related functionality
- `companies/tests.py`: Tests for company and manufacturer models
- `location/tests.py`: Tests for location functionality and GeoDjango features
- Additional test files in other app directories
## Writing New Tests
When adding new features or modifying existing ones, please ensure:
1. All new code is covered by tests
2. Tests follow the existing pattern in related test files
3. Both positive and negative test cases are included
4. Edge cases are considered and tested
## Test Categories
The test suite includes:
- Model Tests: Verify model creation, validation, and methods
- View Tests: Test view responses and template rendering
- Form Tests: Validate form processing and validation
- Integration Tests: Test interactions between components
## Continuous Integration
These tests are run automatically on:
- Pull request creation
- Merges to main branch
- Release tagging
## Troubleshooting
If tests fail:
1. Check the error message and stack trace
2. Verify test database settings
3. Ensure all required dependencies are installed
4. Check for any pending migrations
For any issues, please create a ticket in the issue tracker.
## Development Tips
- Run the development server with `python manage.py runserver` to access the coverage reports at `http://localhost:8000/coverage/`
- Coverage reports are only served in development mode (when DEBUG=True)
- The coverage directory is automatically created when running tests
- Reports are updated each time you run the test suite

133
tests/test_runner.py Normal file
View File

@@ -0,0 +1,133 @@
#!/usr/bin/env python
import os
import sys
import django
from django.conf import settings
from django.test.runner import DiscoverRunner
import coverage
import unittest
def setup_django():
"""Set up Django test environment"""
# Add the project root directory to Python path
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
os***REMOVED***iron.setdefault('DJANGO_SETTINGS_MODULE', 'thrillwiki.settings')
django.setup()
# Use PostGIS for GeoDjango support
settings.DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'test_thrillwiki',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
'TEST': {
'NAME': 'test_thrillwiki',
}
}
}
settings.DEBUG = False
# Skip problematic migrations during tests
settings.MIGRATION_MODULES = {
'parks': None,
'companies': None,
'location': None,
'rides': None,
'reviews': None
}
class CustomTestRunner(DiscoverRunner):
def __init__(self, *args, **kwargs):
self.cov = coverage.Coverage(
source=[
'parks',
'companies',
'location',
'rides',
'reviews'
],
omit=[
'*/migrations/*',
'*/management/*',
'*/admin.py',
'*/apps.py',
'manage.py'
]
)
self.cov.start()
super().__init__(*args, **kwargs)
def setup_databases(self, **kwargs):
"""Set up databases and ensure content types are created"""
old_config = super().setup_databases(**kwargs)
# Create necessary content types
from django.contrib.contenttypes.models import ContentType
from parks.models import Park
from companies.models import Company
ContentType.objects.get_or_create(
app_label='parks',
model='park'
)
ContentType.objects.get_or_create(
app_label='companies',
model='company'
)
return old_config
def run_suite(self, suite, **kwargs):
results = super().run_suite(suite, **kwargs)
self.cov.stop()
self.cov.save()
# Print coverage report
print('\nCoverage Report:')
self.cov.report()
# Generate HTML coverage report
html_dir = os.path.join('tests', 'coverage_html')
self.cov.html_report(directory=html_dir)
print(f'\nDetailed HTML coverage report generated in: {html_dir}')
return results
def run_tests():
# Set up Django
setup_django()
# Initialize test runner
test_runner = CustomTestRunner(
verbosity=2,
interactive=True,
keepdb=True
)
# Define test labels for discovery
test_labels = [
'parks.tests',
'companies.tests',
'location.tests',
'rides.tests',
'reviews.tests'
]
# Run tests and collect results
failures = test_runner.run_tests(test_labels)
return failures
if __name__ == '__main__':
# Create tests directory if it doesn't exist
os.makedirs('tests', exist_ok=True)
os.makedirs(os.path.join('tests', 'coverage_html'), exist_ok=True)
# Run tests and exit with appropriate status code
failures = run_tests()
sys.exit(bool(failures))