mirror of
https://github.com/johndoe6345789/docker-swarm-termina.git
synced 2026-04-27 07:05:22 +00:00
- Add pytest configuration with coverage reporting - Create test suite with 90+ test cases covering: - Authentication endpoints - Container management operations - Command execution functionality - Health checks and utilities - Add GitHub Actions workflow for automated testing - Runs on all pushes and PRs - Tests on Python 3.11 and 3.12 - Enforces 70% code coverage minimum - Validates Docker builds - Include test documentation and setup guides https://claude.ai/code/session_016vkdrUjnsBU2KMifxnJfSn
Backend Tests
Comprehensive test suite for the Docker Swarm Terminal backend API.
Test Structure
tests/
├── conftest.py # Pytest fixtures and configuration
├── test_auth.py # Authentication endpoint tests
├── test_containers.py # Container management tests
├── test_exec.py # Command execution tests
├── test_health.py # Health check tests
└── test_utils.py # Utility function tests
Running Tests
Install Dependencies
pip install -r requirements.txt -r requirements-dev.txt
Run All Tests
pytest
Run with Coverage
pytest --cov=. --cov-report=html --cov-report=term-missing
This will generate an HTML coverage report in htmlcov/index.html.
Run Specific Test Files
pytest tests/test_auth.py
pytest tests/test_containers.py -v
Run Tests by Marker
pytest -m unit # Run only unit tests
pytest -m integration # Run only integration tests
Run with Verbose Output
pytest -v
Test Coverage
Current coverage target: 70%
To check if tests meet the coverage threshold:
coverage run -m pytest
coverage report --fail-under=70
Writing Tests
Test Naming Convention
- Test files:
test_*.py - Test classes:
Test* - Test functions:
test_*
Using Fixtures
Common fixtures available in conftest.py:
app: Flask application instanceclient: Test client for making HTTP requestsauth_token: Valid authentication tokenauth_headers: Authentication headers dictmock_docker_client: Mocked Docker client
Example:
def test_my_endpoint(client, auth_headers):
response = client.get('/api/my-endpoint', headers=auth_headers)
assert response.status_code == 200
Mocking Docker Calls
Use the @patch decorator to mock Docker API calls:
from unittest.mock import patch, MagicMock
@patch('app.get_docker_client')
def test_container_operation(mock_get_client, client, auth_headers):
mock_client = MagicMock()
mock_get_client.return_value = mock_client
# Your test code here
CI/CD Integration
Tests automatically run on:
- Every push to any branch
- Every pull request to main
- Multiple Python versions (3.11, 3.12)
GitHub Actions will fail if:
- Any test fails
- Coverage drops below 70%
- Docker images fail to build
Troubleshooting
Tests Failing Locally
- Ensure all dependencies are installed
- Check Python version (3.11+ required)
- Clear pytest cache:
pytest --cache-clear
Import Errors
Make sure you're running tests from the backend directory:
cd backend
pytest
Coverage Not Updating
Clear coverage data and re-run:
coverage erase
pytest --cov=. --cov-report=term-missing