|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This test suite performs **API integration testing** by making HTTP requests to the running Docker container. Tests validate the external behavior of the API endpoints rather than internal code coverage. |
| 6 | + |
| 7 | +## Running Tests Locally (Recommended) |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +1. **Python 3.11+** installed on your machine |
| 12 | +2. **Docker containers running** for the database and API server: |
| 13 | +```powershell |
| 14 | +docker compose up -d |
| 15 | +``` |
| 16 | + - The API must be running at `http://localhost:5000` |
| 17 | + - Wait a few seconds for the server to be ready |
| 18 | + |
| 19 | +### Setup Local Environment |
| 20 | + |
| 21 | +1. **Create virtual environment** (one-time setup): |
| 22 | +```powershell |
| 23 | +cd server |
| 24 | +python -m venv venv |
| 25 | +.\venv\Scripts\Activate.ps1 |
| 26 | +``` |
| 27 | + |
| 28 | +2. **Install development dependencies** (includes pytest and test tools): |
| 29 | +```powershell |
| 30 | +pip install -r requirements-dev.txt |
| 31 | +``` |
| 32 | + |
| 33 | +3. **Configure VS Code** (optional): |
| 34 | + - Select Python interpreter: `Ctrl+Shift+P` → "Python: Select Interpreter" |
| 35 | + - Choose: `.\server\venv\Scripts\python.exe` |
| 36 | + - Tests will auto-discover in the Testing panel |
| 37 | + |
| 38 | +### Run Tests |
| 39 | + |
| 40 | +```powershell |
| 41 | +cd server |
| 42 | +.\venv\Scripts\Activate.ps1 |
| 43 | +
|
| 44 | +# Run all tests |
| 45 | +pytest |
| 46 | +
|
| 47 | +# Run with verbose output |
| 48 | +pytest -v |
| 49 | +
|
| 50 | +# Run with detailed output and stop on first failure |
| 51 | +pytest -v -x |
| 52 | +
|
| 53 | +# Run specific test file |
| 54 | +pytest tests/test_api_boards.py -v |
| 55 | +``` |
| 56 | + |
| 57 | +### Note on Coverage |
| 58 | + |
| 59 | +**Coverage reporting is not applicable** for these tests because they make HTTP requests to an external server (Docker container). The tests validate API behavior, not internal code paths. This is the correct approach for API integration testing. |
| 60 | + |
| 61 | +### Run Specific Test Categories |
| 62 | + |
| 63 | +```powershell |
| 64 | +# Run only API tests |
| 65 | +pytest -m api |
| 66 | +
|
| 67 | +# Run only unit tests |
| 68 | +pytest -m unit |
| 69 | +
|
| 70 | +# Run only integration tests |
| 71 | +pytest -m integration |
| 72 | +``` |
| 73 | + |
| 74 | +### Run Specific Test Files |
| 75 | + |
| 76 | +```powershell |
| 77 | +# Test boards API |
| 78 | +pytest tests/test_api_boards.py |
| 79 | +
|
| 80 | +# Test cards API |
| 81 | +pytest tests/test_api_cards.py |
| 82 | +
|
| 83 | +# Test settings API |
| 84 | +pytest tests/test_api_settings.py |
| 85 | +``` |
| 86 | + |
| 87 | +### Run Specific Test Functions |
| 88 | + |
| 89 | +```powershell |
| 90 | +# Run a specific test by name |
| 91 | +pytest tests/test_api_boards.py::TestBoardsAPI::test_create_board |
| 92 | +
|
| 93 | +# Run tests matching a pattern |
| 94 | +pytest -k "test_create" |
| 95 | +
|
| 96 | +# Run with extra verbosity to see request/response details |
| 97 | +pytest -v -s |
| 98 | +``` |
| 99 | + |
| 100 | +## VS Code Integration |
| 101 | + |
| 102 | +Tests are configured to work with VS Code's Testing panel: |
| 103 | + |
| 104 | +1. Ensure Docker containers are running: `docker compose up -d` |
| 105 | +2. Open the Testing view (beaker icon in sidebar) |
| 106 | +3. Tests will auto-discover |
| 107 | +4. Click play buttons to run individual tests or test suites |
| 108 | + |
| 109 | +### VS Code Test Features |
| 110 | + |
| 111 | +- ✅ Run tests from the UI |
| 112 | +- ✅ Debug tests with breakpoints |
| 113 | +- ✅ Auto-discover tests on save |
| 114 | +- ✅ View test results inline |
| 115 | +- ✅ Navigate to failed tests |
| 116 | + |
| 117 | +**Note**: When running tests from VS Code, they may occasionally fail due to timing issues. Running from command line (`pytest tests/`) is more reliable for full test suite execution. |
| 118 | + |
| 119 | +## Test Architecture |
| 120 | + |
| 121 | +These are **API integration tests** that: |
| 122 | +- Make real HTTP requests to `http://localhost:5000` |
| 123 | +- Test the running Docker container, not Python code directly |
| 124 | +- Validate API behavior, status codes, and response data |
| 125 | +- Automatically clean up test data between runs |
| 126 | + |
| 127 | +### Test Isolation Strategy |
| 128 | + |
| 129 | +- **Session cleanup**: Cleans all data before test suite starts |
| 130 | +- **Test cleanup**: Cleans all data after each test completes |
| 131 | +- **Explicit fixtures**: `clean_database` and `isolated_test` for tests needing guaranteed clean state |
| 132 | +- **Data fixtures**: `sample_board`, `sample_column`, `sample_card` create test data as needed |
| 133 | + |
| 134 | +## Test Structure |
| 135 | + |
| 136 | +``` |
| 137 | +server/tests/ |
| 138 | +├── __init__.py # Test package |
| 139 | +├── conftest.py # Pytest fixtures and configuration |
| 140 | +├── test_api_boards.py # Board and column API endpoint tests |
| 141 | +├── test_api_cards.py # Card API endpoint tests |
| 142 | +└── test_api_settings.py # Settings API endpoint tests |
| 143 | +``` |
| 144 | + |
| 145 | +## Available Fixtures |
| 146 | + |
| 147 | +Defined in `conftest.py`: |
| 148 | + |
| 149 | +- `api_client` - Base URL for API (`http://localhost:5000`) |
| 150 | +- `clean_database` - Ensures database is empty before test runs |
| 151 | +- `isolated_test` - Ensures database is clean before fixtures create data |
| 152 | +- `sample_board` - Creates a test board via API |
| 153 | +- `sample_column` - Creates a test column via API (requires `sample_board`) |
| 154 | +- `sample_card` - Creates a test card via API (requires `sample_column`) |
| 155 | + |
| 156 | +## Writing New Tests |
| 157 | + |
| 158 | +### Example API Test |
| 159 | + |
| 160 | +```python |
| 161 | +import requests |
| 162 | +import pytest |
| 163 | + |
| 164 | +@pytest.mark.api |
| 165 | +class TestMyAPI: |
| 166 | + def test_my_endpoint(self, api_client): |
| 167 | + """Test description.""" |
| 168 | + response = requests.get(f'{api_client}/api/my-endpoint') |
| 169 | + assert response.status_code == 200 |
| 170 | + data = response.json() |
| 171 | + assert data['success'] is True |
| 172 | + |
| 173 | + def test_with_clean_database(self, api_client, clean_database): |
| 174 | + """Test that needs empty database.""" |
| 175 | + response = requests.get(f'{api_client}/api/boards') |
| 176 | + assert response.json()['boards'] == [] |
| 177 | + |
| 178 | + def test_with_data(self, api_client, isolated_test, sample_board): |
| 179 | + """Test with exactly one board.""" |
| 180 | + response = requests.get(f'{api_client}/api/boards') |
| 181 | + assert len(response.json()['boards']) == 1 |
| 182 | +``` |
| 183 | + |
| 184 | +## Continuous Integration |
| 185 | + |
| 186 | +Tests can be integrated into CI/CD pipelines: |
| 187 | + |
| 188 | +```yaml |
| 189 | +# Example GitHub Actions workflow |
| 190 | +- name: Start services |
| 191 | + run: docker compose up -d |
| 192 | + |
| 193 | +- name: Wait for API |
| 194 | + run: | |
| 195 | + timeout 30 bash -c 'until curl -f http://localhost:5000/api/version; do sleep 1; done' |
| 196 | +
|
| 197 | +- name: Setup Python |
| 198 | + uses: actions/setup-python@v4 |
| 199 | + with: |
| 200 | + python-version: '3.11' |
| 201 | + |
| 202 | +- name: Install dependencies |
| 203 | + run: | |
| 204 | + cd server |
| 205 | + pip install -r requirements-dev.txt |
| 206 | +
|
| 207 | +- name: Run tests |
| 208 | + run: | |
| 209 | + cd server |
| 210 | + pytest -v |
| 211 | +``` |
| 212 | +
|
| 213 | +## Troubleshooting |
| 214 | +
|
| 215 | +### API Connection Issues |
| 216 | +
|
| 217 | +If tests fail with connection errors: |
| 218 | +```powershell |
| 219 | +# Check if API is running |
| 220 | +curl http://localhost:5000/api/version |
| 221 | + |
| 222 | +# Restart containers |
| 223 | +docker compose restart |
| 224 | + |
| 225 | +# View logs |
| 226 | +docker compose logs server |
| 227 | +``` |
| 228 | + |
| 229 | +### Tests Fail Randomly in VS Code |
| 230 | + |
| 231 | +The command line is more reliable. Run tests via: |
| 232 | +```powershell |
| 233 | +cd server |
| 234 | +pytest tests/ -v |
| 235 | +``` |
| 236 | + |
| 237 | +### Database State Issues |
| 238 | + |
| 239 | +Tests automatically clean up, but if you need to manually reset: |
| 240 | +```powershell |
| 241 | +# Restart database container |
| 242 | +docker compose restart db |
| 243 | +
|
| 244 | +# Or recreate all containers |
| 245 | +docker compose down |
| 246 | +docker compose up -d |
| 247 | +``` |
| 248 | + |
| 249 | +### Permission Errors on Windows |
| 250 | + |
| 251 | +If you see `.coverage` permission errors: |
| 252 | +- This is expected - coverage doesn't work for API tests |
| 253 | +- Tests will still run successfully |
| 254 | +- You can disable coverage in `pytest.ini` if it bothers you |
0 commit comments