|
| 1 | +# Pytest Setup and CI/CD Integration |
| 2 | + |
| 3 | +## Local Testing |
| 4 | + |
| 5 | +### Installation |
| 6 | + |
| 7 | +Install development dependencies including pytest: |
| 8 | + |
| 9 | +```bash |
| 10 | +pip install -e ".[dev]" |
| 11 | +``` |
| 12 | + |
| 13 | +### Running Tests |
| 14 | + |
| 15 | +Run all tests: |
| 16 | + |
| 17 | +```bash |
| 18 | +pytest |
| 19 | +``` |
| 20 | + |
| 21 | +Run tests with coverage report: |
| 22 | + |
| 23 | +```bash |
| 24 | +pytest --cov=jsweb --cov-report=html |
| 25 | +``` |
| 26 | + |
| 27 | +Run specific test file: |
| 28 | + |
| 29 | +```bash |
| 30 | +pytest Tests/test_routing.py -v |
| 31 | +``` |
| 32 | + |
| 33 | +Run tests with specific marker: |
| 34 | + |
| 35 | +```bash |
| 36 | +pytest -m unit |
| 37 | +pytest -m integration |
| 38 | +pytest -m slow |
| 39 | +``` |
| 40 | + |
| 41 | +Run tests matching a pattern: |
| 42 | + |
| 43 | +```bash |
| 44 | +pytest -k "test_form" -v |
| 45 | +``` |
| 46 | + |
| 47 | +### Available Test Markers |
| 48 | + |
| 49 | +- `@pytest.mark.unit` - Unit tests |
| 50 | +- `@pytest.mark.integration` - Integration tests |
| 51 | +- `@pytest.mark.slow` - Slow running tests |
| 52 | +- `@pytest.mark.asyncio` - Async tests |
| 53 | +- `@pytest.mark.forms` - Form validation tests |
| 54 | +- `@pytest.mark.routing` - Routing tests |
| 55 | +- `@pytest.mark.database` - Database tests |
| 56 | +- `@pytest.mark.security` - Security tests |
| 57 | + |
| 58 | +### Coverage Reports |
| 59 | + |
| 60 | +After running tests with `--cov`, view the HTML coverage report: |
| 61 | + |
| 62 | +```bash |
| 63 | +# On Windows |
| 64 | +start htmlcov/index.html |
| 65 | + |
| 66 | +# On Linux/Mac |
| 67 | +open htmlcov/index.html |
| 68 | +``` |
| 69 | + |
| 70 | +## CI/CD Integration |
| 71 | + |
| 72 | +### GitHub Actions Workflow |
| 73 | + |
| 74 | +The project includes a GitHub Actions workflow (`.github/workflows/tests.yml`) that: |
| 75 | + |
| 76 | +1. **Tests Job** - Runs tests on multiple Python versions (3.8-3.12) |
| 77 | + - Installs dependencies |
| 78 | + - Runs pytest with coverage |
| 79 | + - Uploads coverage to Codecov |
| 80 | + - Archives coverage reports as artifacts |
| 81 | + |
| 82 | +2. **Lint Job** - Checks code quality |
| 83 | + - Black (code formatting) |
| 84 | + - isort (import sorting) |
| 85 | + - Flake8 (linting) |
| 86 | + - MyPy (type checking) |
| 87 | + |
| 88 | +3. **Security Job** - Scans for security issues |
| 89 | + - Bandit (security analysis) |
| 90 | + - Safety (dependency vulnerabilities) |
| 91 | + |
| 92 | +### Workflow Triggers |
| 93 | + |
| 94 | +The workflow runs automatically on: |
| 95 | + |
| 96 | +- Push to `main` and `develop` branches |
| 97 | +- Pull requests to `main` and `develop` branches |
| 98 | + |
| 99 | +### Codecov Integration |
| 100 | + |
| 101 | +Coverage reports are automatically uploaded to Codecov. Add a `CODECOV_TOKEN` secret in your GitHub repository settings for authenticated uploads (optional). |
| 102 | + |
| 103 | +## Configuration Files |
| 104 | + |
| 105 | +### pytest.ini |
| 106 | + |
| 107 | +Main pytest configuration file with: |
| 108 | +- Test discovery patterns |
| 109 | +- Output options |
| 110 | +- Test markers |
| 111 | +- Asyncio mode settings |
| 112 | + |
| 113 | +### pyproject.toml |
| 114 | + |
| 115 | +Contains additional pytest and coverage configuration: |
| 116 | +- `[tool.pytest.ini_options]` - Pytest settings |
| 117 | +- `[tool.coverage.run]` - Coverage collection settings |
| 118 | +- `[tool.coverage.report]` - Coverage report options |
| 119 | + |
| 120 | +### .coveragerc |
| 121 | + |
| 122 | +Detailed coverage configuration: |
| 123 | +- Source paths |
| 124 | +- Files to omit |
| 125 | +- Excluded lines |
| 126 | +- Report formats |
| 127 | + |
| 128 | +## Pre-commit Hooks |
| 129 | + |
| 130 | +To run tests and linting before commits, set up pre-commit: |
| 131 | + |
| 132 | +```bash |
| 133 | +pre-commit install |
| 134 | +pre-commit run --all-files |
| 135 | +``` |
| 136 | + |
| 137 | +The `.pre-commit-config.yaml` should include pytest and other linting tools. |
| 138 | + |
| 139 | +## Tips for Writing Tests |
| 140 | + |
| 141 | +### Basic Test Structure |
| 142 | + |
| 143 | +```python |
| 144 | +import pytest |
| 145 | +from jsweb import App |
| 146 | + |
| 147 | +@pytest.mark.unit |
| 148 | +def test_app_creation(): |
| 149 | + """Test that an app can be created.""" |
| 150 | + app = App(__name__) |
| 151 | + assert app is not None |
| 152 | +``` |
| 153 | + |
| 154 | +### Using Fixtures |
| 155 | + |
| 156 | +```python |
| 157 | +@pytest.mark.unit |
| 158 | +def test_with_app(app): |
| 159 | + """Test using the app fixture.""" |
| 160 | + assert app is not None |
| 161 | + |
| 162 | +@pytest.mark.unit |
| 163 | +def test_with_config(config): |
| 164 | + """Test using the config fixture.""" |
| 165 | + assert config.TESTING is True |
| 166 | +``` |
| 167 | + |
| 168 | +### Async Tests |
| 169 | + |
| 170 | +```python |
| 171 | +@pytest.mark.asyncio |
| 172 | +async def test_async_operation(): |
| 173 | + """Test async code.""" |
| 174 | + result = await some_async_function() |
| 175 | + assert result is not None |
| 176 | +``` |
| 177 | + |
| 178 | +### Parametrized Tests |
| 179 | + |
| 180 | +```python |
| 181 | +@pytest.mark.parametrize("input,expected", [ |
| 182 | + ("test", "test"), |
| 183 | + ("TEST", "test"), |
| 184 | + ("Test", "test"), |
| 185 | +]) |
| 186 | +def test_string_lowercase(input, expected): |
| 187 | + """Test string lowercasing with multiple inputs.""" |
| 188 | + assert input.lower() == expected |
| 189 | +``` |
| 190 | + |
| 191 | +## Troubleshooting |
| 192 | + |
| 193 | +### ImportError: No module named 'jsweb' |
| 194 | + |
| 195 | +Install the package in development mode: |
| 196 | + |
| 197 | +```bash |
| 198 | +pip install -e . |
| 199 | +``` |
| 200 | + |
| 201 | +### Coverage not showing results |
| 202 | + |
| 203 | +Make sure to use: |
| 204 | + |
| 205 | +```bash |
| 206 | +pytest --cov=jsweb |
| 207 | +``` |
| 208 | + |
| 209 | +### Tests not being discovered |
| 210 | + |
| 211 | +Check that test files follow the pattern: `test_*.py` and test functions start with `test_` |
| 212 | + |
| 213 | +### Async test issues |
| 214 | + |
| 215 | +Ensure pytest-asyncio is installed: |
| 216 | + |
| 217 | +```bash |
| 218 | +pip install pytest-asyncio |
| 219 | +``` |
0 commit comments