Testing¶
Comprehensive testing documentation for Printernizer. This section covers all aspects of testing, from unit tests to end-to-end testing strategies.
Testing Overview¶
Printernizer maintains high code quality through:
- Unit Tests - Individual component testing
- Integration Tests - Service and API testing
- End-to-End Tests - Full workflow testing
- Coverage Reporting - Code coverage tracking
Quick Start¶
# Run all tests
pytest
# Run with coverage
pytest --cov=src tests/
# Run specific test file
pytest tests/test_printer_service.py
# Run with detailed output
pytest -v
# Generate HTML coverage report
pytest --cov=src --cov-report=html tests/
Available Documentation¶
- Automated Job Creation Testing - Testing automated job creation workflows
- E2E Test Architecture - End-to-end testing architecture and structure
- E2E Testing Quick Reference - Quick reference for E2E testing
- E2E Test Analysis - Analysis of E2E test results
- Coverage Report Guide - Generating and understanding coverage reports
- Playwright Setup - Playwright setup documentation
- Testing Guide - Comprehensive testing guide for auto-download system
Test Structure¶
tests/
├── unit/ # Unit tests
│ ├── test_services/ # Service tests
│ ├── test_models/ # Model tests
│ └── test_printers/ # Printer integration tests
├── integration/ # Integration tests
│ ├── test_api/ # API endpoint tests
│ └── test_workflows/ # Full workflow tests
├── e2e/ # End-to-end tests
│ └── test_scenarios/ # Complete user scenarios
└── conftest.py # Pytest configuration
Testing Philosophy¶
Test Pyramid¶
/\\
/E2E\\ Few comprehensive end-to-end tests
/------\\
/ INT \\ More integration tests
/----------\\
/ UNIT \\ Many focused unit tests
/--------------\\
Best Practices¶
- Write tests first (TDD where appropriate)
- Test behavior, not implementation
- Keep tests independent
- Use descriptive test names
- Mock external dependencies
- Aim for >80% coverage
Running Tests¶
Basic Commands¶
# All tests
pytest
# Specific test file
pytest tests/test_printer_service.py
# Specific test function
pytest tests/test_printer_service.py::test_add_printer
# With coverage
pytest --cov=src tests/
# Generate coverage reports
pytest --cov=src --cov-report=html --cov-report=term tests/
Watch Mode¶
Writing Tests¶
Unit Test Example¶
import pytest
from src.services.printer_service import PrinterService
@pytest.mark.asyncio
async def test_add_printer():
"""Test adding a new printer."""
service = PrinterService()
printer_data = {
"id": "test123",
"name": "Test Printer",
"type": "bambu_lab"
}
printer = await service.add_printer(printer_data)
assert printer.id == "test123"
assert printer.name == "Test Printer"
assert printer.type == "bambu_lab"
Integration Test Example¶
import pytest
from httpx import AsyncClient
from src.main import app
@pytest.mark.asyncio
async def test_get_printers_endpoint():
"""Test GET /api/v1/printers endpoint."""
async with AsyncClient(app=app, base_url="http://test") as client:
response = await client.get("/api/v1/printers")
assert response.status_code == 200
assert "printers" in response.json()
Test Fixtures¶
Common fixtures are available in tests/conftest.py:
@pytest.fixture
async def test_printer():
"""Provide a test printer instance."""
return BambuLabPrinter(
printer_id="test123",
name="Test Printer",
ip="192.168.1.100"
)
@pytest.fixture
async def test_client():
"""Provide a test HTTP client."""
async with AsyncClient(app=app, base_url="http://test") as client:
yield client
Coverage Goals¶
| Component | Target Coverage |
|---|---|
| Services | >90% |
| Models | >85% |
| API Routes | >80% |
| Utilities | >75% |
| Overall | >80% |
Continuous Integration¶
Tests run automatically on:
- Every push to
development - Every pull request
- Before merging to
master
See .github/workflows/ci-cd.yml for CI configuration.
Debugging Tests¶
Enable Logging¶
Run Single Test¶
Use Debugger¶
Performance Testing¶
Load Testing¶
Profiling¶
Test Reports¶
HTML Coverage Report¶
JSON Report¶
HTML Test Report¶
Mocking¶
Mock External Services¶
from unittest.mock import AsyncMock, patch
@pytest.mark.asyncio
async def test_printer_connection():
with patch('src.printers.bambu_lab.mqtt.Client') as mock_mqtt:
mock_mqtt.return_value = AsyncMock()
# Test code here
Test Data¶
Test data and fixtures are located in tests/fixtures/:
tests/fixtures/
├── printers/ # Sample printer configurations
├── jobs/ # Sample job data
├── files/ # Sample file metadata
└── responses/ # Mock API responses
Additional Resources¶
Contributing Tests¶
When contributing:
- Add tests for all new features
- Maintain or improve coverage
- Follow existing test patterns
- Update test documentation
See the CONTRIBUTING.md guide for more details.