Skip to content

Set up Python testing infrastructure with Poetry and pytest #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

llbbl
Copy link

@llbbl llbbl commented Jun 23, 2025

Set Up Python Testing Infrastructure

Summary

This PR establishes a comprehensive testing infrastructure for the Django online education platform using Poetry for dependency management and pytest as the testing framework.

Changes Made

Package Management

  • Migrated to Poetry: Created pyproject.toml with all dependencies from requirements.txt
  • Preserved exact versions: Maintained all existing dependency versions for compatibility
  • Added development dependencies: pytest, pytest-cov, pytest-mock, pytest-django

Testing Configuration

  • pytest settings: Configured in pyproject.toml with:
    • Test discovery patterns for multiple naming conventions
    • Coverage reporting with 80% threshold
    • HTML and XML coverage output formats
    • Custom markers for test categorization (unit, integration, slow)
    • Strict mode and verbose output

Directory Structure

tests/
├── __init__.py
├── conftest.py          # Shared fixtures and configuration
├── unit/                # Unit tests
│   └── __init__.py
├── integration/         # Integration tests
│   └── __init__.py
├── test_setup_validation.py  # Infrastructure validation
└── test_basic_setup.py       # Basic non-Django tests

Test Fixtures (conftest.py)

Created comprehensive fixtures for common testing needs:

  • temp_dir: Temporary directory management
  • mock_config: Mock configuration objects
  • client / authenticated_client: Django test clients
  • test_user / test_superuser: User fixtures
  • mock_request: HTTP request mocking
  • sample_file / sample_image: File upload testing
  • mock_cache: Cache testing
  • captured_emails: Email testing
  • mock_datetime: Time mocking

Additional Setup

  • Updated .gitignore: Added testing artifacts (.coverage, htmlcov/, etc.)
  • Poetry scripts: Added poetry run test and poetry run tests commands
  • Auto-markers: Tests in unit/ and integration/ directories are automatically marked

How to Use

  1. Install dependencies:

    poetry install
  2. Run all tests:

    poetry run test
    # or
    poetry run tests
  3. Run specific test categories:

    poetry run pytest -m unit          # Run only unit tests
    poetry run pytest -m integration   # Run only integration tests
    poetry run pytest -m "not slow"    # Skip slow tests
  4. Generate coverage report:

    poetry run pytest --cov-report=html
    # Open htmlcov/index.html in browser

Important Notes

Python Version Compatibility

⚠️ Django 1.11.28 is not compatible with Python 3.10+

The project uses Django 1.11.28 which has compatibility issues with Python 3.10 and above due to removed collections.Iterator. The pyproject.toml specifies python = ">=3.6,<3.10" to ensure compatibility.

Recommended Python versions: 3.6, 3.7, 3.8, or 3.9

Next Steps

  1. Developers can now start writing tests in the appropriate directories
  2. Tests will automatically use the configured fixtures
  3. Coverage reports will help identify untested code
  4. Consider upgrading Django in a future PR to support modern Python versions

Testing the Setup

The PR includes validation tests to verify the infrastructure:

  • test_setup_validation.py: Comprehensive infrastructure checks
  • test_basic_setup.py: Basic pytest functionality tests

These tests verify that:

  • Directory structure is correct
  • Configuration files exist and are valid
  • Fixtures are accessible
  • Markers work correctly
  • Coverage is configured properly

- Migrate from requirements.txt to Poetry package management
- Add pytest, pytest-cov, and pytest-mock as dev dependencies
- Configure comprehensive test settings in pyproject.toml
- Create organized test directory structure with fixtures
- Set up coverage reporting with 80% threshold
- Update .gitignore with testing artifacts
- Add Poetry script commands for running tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant