Собираем всё вместе: контракты, параллельный запуск, flaky detection, quality gates, matrix. Полный .github/workflows/test.yml
Полный production-ready CI/CD workflow с всеми best practices.
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
workflow_dispatch:
env:
PYTHON_VERSION: "3.11"
jobs:
lint:
name: Lint & Type Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
- name: Install tools
run: pip install ruff mypy bandit safety
- name: Ruff lint
run: ruff check .
- name: Type check
run: mypy src/
- name: Security scan
run: |
bandit -r src/
safety check
unit-tests:
name: Unit Tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install -r requirements.txt pytest pytest-cov pytest-xdist
- name: Run unit tests
run: pytest -m unit -n auto --cov=src --cov-fail-under=80 --cov-report=xml -v
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
needs: unit-tests
strategy:
matrix:
postgres: [14, 15, 16]
services:
postgres:
image: postgres:${{ matrix.postgres }}
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpass
POSTGRES_DB: testdb
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: pip install -r requirements.txt pytest pytest-xdist
- name: Run integration tests
env:
DATABASE_URL: postgresql://testuser:testpass@localhost:5432/testdb
run: pytest -m integration -n auto -v
contract-tests:
name: Contract Tests
runs-on: ubuntu-latest
needs: unit-tests
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install dependencies
run: pip install -r requirements.txt pytest jsonschema
- name: Run contract tests
run: pytest -m contract -v
quality-gate:
name: Quality Gate
runs-on: ubuntu-latest
needs: [lint, unit-tests, integration-tests, contract-tests]
if: always()
steps:
- name: Check all jobs passed
run: |
echo "Lint: ${{ needs.lint.result }}"
echo "Unit: ${{ needs.unit-tests.result }}"
echo "Integration: ${{ needs.integration-tests.result }}"
echo "Contracts: ${{ needs.contract-tests.result }}"
if [[ "${{ needs.lint.result }}" != "success" ]] || \
[[ "${{ needs.unit-tests.result }}" != "success" ]] || \
[[ "${{ needs.integration-tests.result }}" != "success" ]] || \
[[ "${{ needs.contract-tests.result }}" != "success" ]]; then
echo "❌ Quality gate FAILED"
exit 1
fi
echo "✅ Quality gate PASSED"✅ Contract Testing защищает API ✅ CI/CD ускорен до 2-3 минут ✅ Quality gates защищают main ✅ Production-ready workflow
Вы завершили курс "Pytest: Контракты и Production CI/CD"!
Вопросы ещё не добавлены
Вопросы для этой подтемы ещё не добавлены.