Files
Claude 713784a450 Add comprehensive testing infrastructure and CI/CD
- 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
2026-01-31 00:16:18 +00:00
..

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 instance
  • client: Test client for making HTTP requests
  • auth_token: Valid authentication token
  • auth_headers: Authentication headers dict
  • mock_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

  1. Ensure all dependencies are installed
  2. Check Python version (3.11+ required)
  3. 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