Comprehensive Django testing enforcement library that ensures 100% test coverage across views, UI elements, and functions
Project description
Django Test Enforcer ๐ก๏ธ
Comprehensive Django testing enforcement library that ensures 100% test coverage across views, UI elements, and functions.
Django Test Enforcer automatically scans your Django project and enforces test coverage for:
- โ Backend Views (function-based, class-based, generic views)
- โ UI Elements (buttons, forms, links, inputs)
- โ Functions & Utilities (business logic, helpers)
- โ API Endpoints (Django REST Framework)
๐ Features
- Automatic Discovery: Scans your entire Django project to find all testable components
- Coverage Enforcement: Fails your CI/CD build if coverage requirements aren't met
- Test Generation: Auto-generates test stubs for missing coverage
- Pytest Integration: Seamless pytest plugin with custom markers and hooks
- Rich Reports: Beautiful console and HTML reports showing coverage gaps
- Django Commands: Management commands for quick checks and test generation
- Configurable Rules: Fine-grained control over coverage requirements
- UI Testing Support: Playwright and Selenium integration for UI tests
๐ฆ Installation
# Basic installation
pip install django-test-enforcer
# With UI testing support (Playwright)
pip install django-test-enforcer[ui]
# With API testing support (DRF)
pip install django-test-enforcer[api]
# Full installation
pip install django-test-enforcer[all]
โก Quick Start
1. Add to Django Settings
INSTALLED_APPS = [
...
'django_test_enforcer',
]
2. Configure pytest (optional)
# pyproject.toml
[tool.django_test_enforcer]
enforcement_level = "strict" # "strict", "warning", or "off"
min_view_coverage = 100
min_ui_coverage = 90
min_function_coverage = 80
3. Run Tests with Enforcement
# Check coverage without failing
pytest --check-coverage
# Enforce coverage (fails build if not met)
pytest --enforce-coverage
# Generate missing test stubs
pytest --generate-tests
# Show detailed coverage report
pytest --coverage-report
๐ Django Management Commands
# Check current test coverage
python manage.py check_coverage
# Check specific categories
python manage.py check_coverage --views
python manage.py check_coverage --ui
python manage.py check_coverage --functions
# Generate missing tests
python manage.py generate_tests
python manage.py generate_tests --output tests/auto_generated/
# Enforce tests (for CI/CD)
python manage.py enforce_tests --fail-on-missing
๐ฏ Example Output
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Django Test Enforcer Report
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
View Coverage: 85% (34/40 views tested) โ ๏ธ
UI Coverage: 92% (115/125 elements tested) โ
Function Coverage: 78% (156/200 functions tested) โ
API Coverage: 100% (20/20 endpoints tested) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Missing Tests (20 items):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
VIEWS (6):
โ myapp.views.send_notification
โ myapp.views.publish_event
โ myapp.views.dashboard
UI ELEMENTS (10):
โ Button: "Submit" (templates/form.html:45)
โ Form: notification_form (templates/send.html:23)
โ Link: /dashboard/ (templates/home.html:67)
FUNCTIONS (4):
โ utils.helpers.calculate_stats
โ services.notification.process_queue
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Generate missing tests with:
pytest --generate-tests
or
python manage.py generate_tests
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Configuration
Full Configuration Example
[tool.django_test_enforcer]
# Enforcement level: "strict" (fail build), "warning" (show warnings), "off"
enforcement_level = "strict"
# Minimum coverage percentages
min_view_coverage = 100
min_ui_coverage = 90
min_function_coverage = 80
min_api_coverage = 100
# What to scan
scan_views = true
scan_templates = true
scan_functions = true
scan_apis = true
# Excluded paths (glob patterns)
exclude_paths = [
"*/migrations/*",
"*/tests/*",
"*/admin.py",
"*/apps.py",
]
# Test generation
auto_generate_stubs = true
test_output_dir = "tests/auto_generated"
ui_framework = "playwright" # or "selenium"
# Custom rules
[tool.django_test_enforcer.rules]
require_authenticated_tests = true
require_permission_tests = true
require_form_validation_tests = true
require_error_handling_tests = true
require_api_pagination_tests = true
๐ CI/CD Integration
GitHub Actions
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install django-test-enforcer[all]
- name: Run tests with enforcement
run: pytest --enforce-coverage --cov
Jenkins
stage('Test Coverage Enforcement') {
steps {
sh 'pytest --enforce-coverage --junitxml=results.xml'
}
post {
always {
junit 'results.xml'
}
failure {
echo 'Test coverage requirements not met!'
}
}
}
๐จ Generated Test Examples
View Test (Auto-generated)
def test_send_notification_view_get(client, authenticated_user):
"""Test send_notification view GET request"""
client.force_login(authenticated_user)
response = client.get('/messaging/send/')
assert response.status_code == 200
assert 'form' in response.context
def test_send_notification_view_post(client, authenticated_user):
"""Test send_notification view POST request"""
client.force_login(authenticated_user)
data = {
'title': 'Test Notification',
'message': 'Test message',
'priority': 'high'
}
response = client.post('/messaging/send/', data)
assert response.status_code == 302 # Redirect after success
UI Test (Auto-generated)
def test_submit_button_notification_form(page, authenticated_page):
"""Test Submit button in notification form"""
page.goto('/messaging/send/')
page.fill('input[name="title"]', 'Test')
page.fill('textarea[name="message"]', 'Test message')
page.click('button[type="submit"]')
page.wait_for_url('**/dashboard/')
assert 'success' in page.content().lower()
๐ Documentation
Full documentation available at django-test-enforcer.readthedocs.io
๐ค Contributing
Contributions are welcome! Please read our Contributing Guide for details.
# Clone repository
git clone https://github.com/arpansahu/django-test-enforcer.git
cd django-test-enforcer
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install in development mode
pip install -e ".[dev,all]"
# Run tests
pytest
# Run linters
black .
isort .
flake8
mypy django_test_enforcer
๐ License
MIT License - see LICENSE file for details
๐ Star History
๐ฌ Support
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
- ๐ง Email Support
๐ Acknowledgments
Built with โค๏ธ by Arpan Sahu
Inspired by the Django and pytest communities.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_test_enforcer-0.1.0.tar.gz.
File metadata
- Download URL: django_test_enforcer-0.1.0.tar.gz
- Upload date:
- Size: 36.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd7dae32aac06a761b83bf73632dde6f37eeafbe9cceb816d5f3dc6d3b519271
|
|
| MD5 |
1d3d94dcd7ce0f07f548c7e8ca91d4cb
|
|
| BLAKE2b-256 |
a5db453be188a899cd499f4a34d3ff383fd72db4bc45077cb2ea1b6e319cfa14
|
File details
Details for the file django_test_enforcer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_test_enforcer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 38.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
573da42975893391d67671439d5bae9f96bcd63dceba18ac9c9416d57ec07ca0
|
|
| MD5 |
5576d4bf921ef0c42cf79e4fbe787129
|
|
| BLAKE2b-256 |
303b92e927b480ede9a43be8dacd0e7288ff26154ac801923a785332f039cae0
|