This repository has been archived on 2026-02-15. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
breakpilot-pwa/backend/tests/conftest.py
BreakPilot Dev 19855efacc
Some checks failed
Tests / Go Tests (push) Has been cancelled
Tests / Python Tests (push) Has been cancelled
Tests / Integration Tests (push) Has been cancelled
Tests / Go Lint (push) Has been cancelled
Tests / Python Lint (push) Has been cancelled
Tests / Security Scan (push) Has been cancelled
Tests / All Checks Passed (push) Has been cancelled
Security Scanning / Secret Scanning (push) Has been cancelled
Security Scanning / Dependency Vulnerability Scan (push) Has been cancelled
Security Scanning / Go Security Scan (push) Has been cancelled
Security Scanning / Python Security Scan (push) Has been cancelled
Security Scanning / Node.js Security Scan (push) Has been cancelled
Security Scanning / Docker Image Security (push) Has been cancelled
Security Scanning / Security Summary (push) Has been cancelled
CI/CD Pipeline / Go Tests (push) Has been cancelled
CI/CD Pipeline / Python Tests (push) Has been cancelled
CI/CD Pipeline / Website Tests (push) Has been cancelled
CI/CD Pipeline / Linting (push) Has been cancelled
CI/CD Pipeline / Security Scan (push) Has been cancelled
CI/CD Pipeline / Docker Build & Push (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Deploy to Staging (push) Has been cancelled
CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / CI Summary (push) Has been cancelled
ci/woodpecker/manual/build-ci-image Pipeline was successful
ci/woodpecker/manual/main Pipeline failed
feat: BreakPilot PWA - Full codebase (clean push without large binaries)
All services: admin-v2, studio-v2, website, ai-compliance-sdk,
consent-service, klausur-service, voice-service, and infrastructure.
Large PDFs and compiled binaries excluded via .gitignore.
2026-02-11 13:25:58 +01:00

130 lines
5.5 KiB
Python

"""
Pytest configuration for backend tests.
This file is loaded BEFORE any test modules are imported,
which allows us to set environment variables that are checked at import time.
CI Environment Variables:
- CI=true: Running in CI environment (auto-detected)
- SKIP_INTEGRATION_TESTS=true: Skip tests that require external services
- SKIP_INTEGRATION_TESTS=false: Run integration tests with Docker Compose services
- SKIP_DB_TESTS=true: Skip tests that require PostgreSQL
- SKIP_WEASYPRINT_TESTS=true: Skip tests that require WeasyPrint
Integration Test Environment:
When SKIP_INTEGRATION_TESTS=false, the tests will use the Docker Compose test environment:
- PostgreSQL: postgres-test:5432 (inside Docker network)
- Valkey/Redis: valkey-test:6379
- Consent Service: consent-service-test:8081
- Backend: backend-test:8000
"""
import os
import sys
from pathlib import Path
import pytest
# Add backend directory to Python path so that modules like classroom_engine
# can be imported correctly during test collection
backend_dir = Path(__file__).parent.parent
if str(backend_dir) not in sys.path:
sys.path.insert(0, str(backend_dir))
# Detect CI environment
IS_CI = os.environ.get("CI", "").lower() in ("true", "1", "woodpecker")
# =============================================================================
# Integration Test Environment Detection
# =============================================================================
# Check if we should run integration tests (SKIP_INTEGRATION_TESTS=false means run them)
IS_INTEGRATION_ENV = os.environ.get("SKIP_INTEGRATION_TESTS", "").lower() == "false"
if IS_INTEGRATION_ENV:
# Use Docker Compose test container URLs when in integration mode
# These URLs work inside the Docker network (container names as hostnames)
os.environ.setdefault("DATABASE_URL",
"postgresql://breakpilot:breakpilot_test@postgres-test:5432/breakpilot_test")
os.environ.setdefault("CONSENT_SERVICE_URL",
"http://consent-service-test:8081")
os.environ.setdefault("VALKEY_URL",
"redis://valkey-test:6379")
os.environ.setdefault("REDIS_URL",
"redis://valkey-test:6379")
os.environ.setdefault("SMTP_HOST", "mailpit-test")
os.environ.setdefault("SMTP_PORT", "1025")
print("[conftest.py] Integration test environment detected - using Docker Compose services")
else:
# Set DATABASE_URL before any modules are imported
# This prevents RuntimeError from rbac_api.py during test collection
if "DATABASE_URL" not in os.environ:
os.environ["DATABASE_URL"] = "postgresql://test:test@localhost:5432/test_db"
# =============================================================================
# Standard Test Configuration
# =============================================================================
# Set other required environment variables for testing
os.environ.setdefault("ENVIRONMENT", "testing")
os.environ.setdefault("JWT_SECRET", "test-secret-key-for-testing-only")
# Teacher Dashboard API - disable auth for testing
os.environ.setdefault("TEACHER_REQUIRE_AUTH", "false")
# Disable database for unit tests (use in-memory fallbacks)
os.environ.setdefault("GAME_USE_DATABASE", "false")
# In CI, auto-enable skips for tests that require external services
# UNLESS we're explicitly running integration tests
if IS_CI and not IS_INTEGRATION_ENV:
os.environ.setdefault("SKIP_INTEGRATION_TESTS", "true")
os.environ.setdefault("SKIP_DB_TESTS", "true")
os.environ.setdefault("SKIP_WEASYPRINT_TESTS", "true")
def pytest_configure(config):
"""Register custom markers and configure pytest."""
config.addinivalue_line(
"markers", "integration: marks tests as integration tests (require external services)"
)
config.addinivalue_line(
"markers", "requires_postgres: marks tests that require PostgreSQL database"
)
config.addinivalue_line(
"markers", "requires_weasyprint: marks tests that require WeasyPrint system libraries"
)
def pytest_collection_modifyitems(config, items):
"""
Automatically skip tests based on markers and environment.
This runs after test collection and can skip tests based on:
- Environment variables (SKIP_INTEGRATION_TESTS, SKIP_DB_TESTS, etc.)
- Marker presence (integration, requires_postgres, requires_weasyprint)
"""
skip_integration = os.environ.get("SKIP_INTEGRATION_TESTS", "").lower() in ("true", "1")
skip_db = os.environ.get("SKIP_DB_TESTS", "").lower() in ("true", "1")
skip_weasyprint = os.environ.get("SKIP_WEASYPRINT_TESTS", "").lower() in ("true", "1")
skip_integration_marker = pytest.mark.skip(reason="Skipped: SKIP_INTEGRATION_TESTS=true")
skip_db_marker = pytest.mark.skip(reason="Skipped: SKIP_DB_TESTS=true (no PostgreSQL in CI)")
skip_weasyprint_marker = pytest.mark.skip(reason="Skipped: SKIP_WEASYPRINT_TESTS=true (no libgobject in CI)")
for item in items:
# Skip integration tests
if skip_integration and "integration" in item.keywords:
item.add_marker(skip_integration_marker)
# Skip tests requiring PostgreSQL
if skip_db and "requires_postgres" in item.keywords:
item.add_marker(skip_db_marker)
# Skip tests requiring WeasyPrint
if skip_weasyprint and "requires_weasyprint" in item.keywords:
item.add_marker(skip_weasyprint_marker)
# Auto-detect test_integration folder and skip
if skip_integration and "test_integration" in str(item.fspath):
item.add_marker(skip_integration_marker)