This document describes the testing infrastructure for the swecc-core monorepo.
The monorepo contains Python tests for the following services:
- AI Service (
services/ai) - FastAPI service with pytest - Bot Service (
services/bot) - Discord bot with pytest - Sockets Service (
services/sockets) - WebSocket service with pytest - Server Service (
services/server) - Django application with Django test framework
- Python 3.9 or higher
- pip (Python package installer)
- Run the setup script to create a virtual environment and install all test dependencies:
./setup_tests.shThis script will:
- Create a Python virtual environment in
venv/ - Install all test dependencies for all services
- Set up the environment for running tests
- Activate the virtual environment:
source venv/bin/activateImportant: You must activate the virtual environment every time you want to run tests in a new terminal session.
Once your environment is set up, you can run tests using either the Makefile or the test runner script:
# Run all tests
make test
# Run tests for specific services
make test-ai
make test-bot
make test-sockets
make test-server
# Run tests with coverage
make test-coverage
# Using the test runner directly
./run_tests.sh # All services
./run_tests.sh ai bot # Specific services
./run_tests.sh --coverage # With coverage
./run_tests.sh -v ai # Verbose outputcd services/ai
pip install -r requirements-test.txt
pytest tests/ -vTest Configuration: services/ai/pytest.ini
Coverage: Configured to cover the app module
cd services/bot
pip install -r requirements-test.txt
pytest tests/ -vTest Configuration: services/bot/pytest.ini
Coverage: Configured with branch coverage enabled
cd services/sockets
pip install -r requirements-test.txt
pytest tests/ -vTest Configuration: services/sockets/pytest.ini
Coverage: Configured to cover the app module
cd services/server
python run_tests.pyThe server uses a custom test runner (run_tests.py) that:
- Configures Django to use SQLite in-memory database for testing
- Sets up required environment variables
- Runs tests for
resume_reviewandcontentManageapps
Tests run automatically on all pull requests via GitHub Actions (.github/workflows/ci.yml).
The CI pipeline includes:
- Lint Job - Runs pre-commit hooks on all services
- Test Job - Runs the full test suite for all Python services in parallel
- Build Job - Builds Docker images for changed services
- Runs on:
ubuntu-latest - Python version:
3.11 - Strategy: Matrix build for each service (parallel execution)
- Coverage: Generates coverage reports and uploads to Codecov (optional)
Each service follows this structure:
services/<service>/
├── tests/
│ ├── __init__.py
│ ├── conftest.py # Pytest fixtures and configuration
│ └── test_*.py # Test files
├── pytest.ini # Pytest configuration (for pytest-based services)
├── requirements-test.txt # Test dependencies
└── run_tests.sh # Optional service-specific test runner
Tests should follow pytest conventions:
- Test files:
test_*.py - Test functions:
test_* - Test classes:
Test*
Example:
def test_example():
assert True
class TestExample:
def test_method(self):
assert TrueTests should follow Django conventions:
- Test files:
tests.pyortests/directory in each app - Test classes: Inherit from
django.test.TestCase
make test- Run all testsmake test-ai- Run AI service testsmake test-bot- Run bot service testsmake test-sockets- Run sockets service testsmake test-server- Run server testsmake test-coverage- Run all tests with coveragemake install-test-deps- Install test dependencies for all servicesmake clean-test- Clean test artifacts and cache
This means you haven't activated the virtual environment or haven't installed dependencies.
Solution:
-
Make sure you've run the setup script:
./setup_tests.sh
-
Activate the virtual environment:
source venv/bin/activate -
Verify pytest is available:
python3 -m pytest --version
If you see a version number, you're good to go!
Use the setup script which handles this:
./setup_tests.shEnsure you have the latest dependencies:
source venv/bin/activate
make install-test-depsMake sure pytest-cov is installed:
pip install pytest-cov- Always run tests before pushing - Use
make testto verify all tests pass - Write tests for new features - Maintain or improve code coverage
- Use fixtures - Define reusable test fixtures in
conftest.py - Mock external dependencies - Don't make real API calls or database connections in tests
- Keep tests fast - Tests should run quickly to encourage frequent execution
- Use descriptive test names - Test names should clearly describe what they test