mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 07:31:07 -05:00
series of tests added with built-in django test support
This commit is contained in:
133
tests/test_runner.py
Normal file
133
tests/test_runner.py
Normal 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))
|
||||
Reference in New Issue
Block a user