Skip to content

fix: Resolve pytest test collection issues #4

fix: Resolve pytest test collection issues

fix: Resolve pytest test collection issues #4

Workflow file for this run

name: CI/CD Pipeline
on:
push:
branches: [ main, develop, feature/*, fix/* ]
pull_request:
branches: [ main, develop ]
permissions:
contents: read
pull-requests: write
checks: write
actions: read
security-events: write
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.10.x, 3.11.x, 3.12.x]
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: test_backend
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
redis:
image: redis:7
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 6379:6379
steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Set up environment variables
run: |
echo "DJANGO_DEBUG=False" >> $GITHUB_ENV
echo "SECRET_KEY=test-secret-key-for-ci" >> $GITHUB_ENV
echo "ALLOWED_HOSTS=localhost,127.0.0.1" >> $GITHUB_ENV
echo "DATABASE_TYPE=pgsql" >> $GITHUB_ENV
echo "DATABASE_USER=postgres" >> $GITHUB_ENV
echo "DATABASE_PASSWORD=postgres" >> $GITHUB_ENV
echo "DATABASE_HOST=localhost" >> $GITHUB_ENV
echo "DATABASE_PORT=5432" >> $GITHUB_ENV
echo "DATABASE_NAME=test_backend" >> $GITHUB_ENV
echo "REDIS_HOST=localhost" >> $GITHUB_ENV
echo "REDIS_PORT=6379" >> $GITHUB_ENV
echo "REDIS_DB=0" >> $GITHUB_ENV
- name: Run migrations
run: |
python manage.py migrate
- name: Run Django system checks
run: |
python manage.py check
- name: Run tests with pytest
run: |
pytest --verbose --tb=short --cov=. --cov-report=xml --cov-report=html
- name: Upload coverage to Codecov
if: matrix.python-version == '3.11.x'
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
- name: Run Django tests (fallback)
if: failure()
run: |
python manage.py test
- name: Post test results to PR
if: github.event_name == 'pull_request' && matrix.python-version == '3.11.x'
uses: actions/github-script@v7
with:
script: |
const comment = `
## 🧪 Test Results (Python ${{ matrix.python-version }})
✅ **Tests Status**: ${{ job.status }}
### Test Summary
- All tests executed successfully
- Django system checks passed
- Database migrations verified
- Coverage report generated
📊 **Coverage Report**: Available in artifacts
`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.x
- name: Install linting dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 black isort
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Check code formatting with black
run: |
black --check --diff .
- name: Check import sorting with isort
run: |
isort --check-only --diff .
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.x
- name: Install security scanning dependencies
run: |
python -m pip install --upgrade pip
pip install bandit safety
- name: Run security scan with bandit
run: |
bandit -r . -f json -o bandit-report.json || true
bandit -r . --severity-level medium
- name: Run dependency security check with safety
run: |
safety check --json --output safety-report.json || true
safety check
docker:
runs-on: ubuntu-latest
needs: [test, lint]
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: |
docker build -t seccodesmithbackend:latest .
- name: Test Docker image
run: |
docker run --rm seccodesmithbackend:latest python manage.py check