Skip to main content

Comprehensive Django testing enforcement library that ensures 100% test coverage across views, UI elements, and functions

Project description

Django Test Enforcer ๐Ÿ›ก๏ธ

PyPI version Python Versions Django Versions License Tests Coverage

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

Star History Chart

๐Ÿ’ฌ Support

๐Ÿ™ Acknowledgments

Built with โค๏ธ by Arpan Sahu

Inspired by the Django and pytest communities.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_test_enforcer-0.1.0.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_test_enforcer-0.1.0-py3-none-any.whl (38.6 kB view details)

Uploaded Python 3

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

Hashes for django_test_enforcer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bd7dae32aac06a761b83bf73632dde6f37eeafbe9cceb816d5f3dc6d3b519271
MD5 1d3d94dcd7ce0f07f548c7e8ca91d4cb
BLAKE2b-256 a5db453be188a899cd499f4a34d3ff383fd72db4bc45077cb2ea1b6e319cfa14

See more details on using hashes here.

File details

Details for the file django_test_enforcer-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_test_enforcer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 573da42975893391d67671439d5bae9f96bcd63dceba18ac9c9416d57ec07ca0
MD5 5576d4bf921ef0c42cf79e4fbe787129
BLAKE2b-256 303b92e927b480ede9a43be8dacd0e7288ff26154ac801923a785332f039cae0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page