# 1. Install test dependencies
pipenv install --dev
# 2. Create test database
psql -U security -h 192.168.1.150 -c "CREATE DATABASE ospf_test;"
# 3. Verify setup
pytest --collect-only# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_models_user.py
# Run specific test
pytest tests/test_models_user.py::TestUserModel::test_user_creation
# Run with coverage
pytest --cov=api --cov=app --cov-report=term-missing# Test all models
pytest tests/test_models_*.py
# Test all API endpoints
pytest tests/test_api_*.py
# Test web controllers
pytest tests/test_web_controllers.py
# Test authentication
pytest tests/test_api_authentication.py
# Test CSV import
pytest tests/test_api_transaction.py::TestTransactionCSVImporttests/
├── conftest.py # Fixtures and configuration
│
├── Model Tests (Database Layer)
├── test_models_user.py # 13 tests
├── test_models_institution.py # 17 tests
├── test_models_categories.py # 15 tests
├── test_models_transaction.py # 14 tests
│
├── API Tests (REST Endpoints)
├── test_api_authentication.py # 12 tests
├── test_api_institution.py # 13 tests
├── test_api_categories.py # 11 tests
├── test_api_transaction.py # 19 tests
│
└── Web Tests (UI Controllers)
└── test_web_controllers.py # 16 tests
Total: ~130+ tests
# Application
app # Flask app
client # Test client
authenticated_client # Logged-in client
# Models
test_user # User account
test_institution # Bank/institution
test_account # Checking account
test_categories_type # Expense type
test_categories_group # Groceries group
test_category # Walmart category
test_transaction # Sample transaction
# Utilities
tmp_path # Temporary directory
sample_csv_file # CSV file for testing# Generate HTML coverage report
pytest --cov=api --cov=app --cov-report=html
# Open coverage report
open htmlcov/index.html # macOS
xdg-open htmlcov/index.html # Linux# Check PostgreSQL is running
pg_isctl status
# Verify connection
psql -U security -h 192.168.1.150 -c "SELECT 1;"psql -U security -h 192.168.1.150 -d ospf_test \
-c "DROP SCHEMA public CASCADE; CREATE SCHEMA public;"# Make sure you're in project root
cd /path/to/OSPF
# Activate virtual environment
pipenv shell
# Reinstall dependencies
pipenv install --dev# tests/test_my_feature.py
class TestMyFeature:
"""Test my new feature"""
def test_success_case(self, client, test_user):
"""Test successful operation"""
# Arrange
data = {'key': 'value'}
# Act
response = client.post('/api/endpoint', json=data)
# Assert
assert response.status_code == 200
def test_error_case(self, client):
"""Test error handling"""
response = client.post('/api/endpoint', json={})
assert response.status_code == 400pytest tests/test_my_feature.py -vTests can be integrated into CI/CD pipelines:
# .github/workflows/test.yml
- name: Run tests
run: pipenv run pytest --cov=api --cov=app- ✅ Read full documentation:
tests/README.md - ✅ Explore existing tests for examples
- ✅ Run tests before committing code
- ✅ Maintain >90% coverage
- ✅ Write tests for new features
| Command | Description |
|---|---|
pytest |
Run all tests |
pytest -v |
Verbose output |
pytest -s |
Show print statements |
pytest -x |
Stop on first failure |
pytest -k "user" |
Run tests matching "user" |
pytest --lf |
Run last failed tests |
pytest --cov |
Run with coverage |
pytest --collect-only |
List all tests without running |
Need help? Check tests/README.md for detailed documentation.