Testing Guide
This guide explains the testing approach and patterns used in ReversePilot.
Testing Framework
The project uses pytest for testing.
Running Tests
Run All Tests
pytest
Run Specific Test File
pytest tests/test_models.py
Run with Coverage
pytest --cov
Run with Verbose Output
pytest -v
Test Structure
Test Locations
Tests are located in:
company/tests.pyloan/tests.pydocuments/tests.pyapps/*/tests.py
Test Organization
import pytest
from django.test import TestCase
from company.models import User, Company
class UserModelTest(TestCase):
def setUp(self):
"""Set up test data."""
self.company = Company.objects.create(name="Test Company")
def test_user_creation(self):
"""Test user creation."""
user = User.objects.create(
email="test@example.com",
company=self.company
)
self.assertEqual(user.email, "test@example.com")
Testing Patterns
Model Tests
def test_model_creation():
"""Test model creation."""
obj = Model.objects.create(field="value")
assert obj.field == "value"
View Tests
def test_api_endpoint(client):
"""Test API endpoint."""
response = client.get('/api/endpoint/')
assert response.status_code == 200
Service Tests
def test_calculation_service():
"""Test calculation service."""
result = calculation_service.calculate(data)
assert result is not None
Fixtures
Database Fixtures
@pytest.fixture
def company():
return Company.objects.create(name="Test Company")
@pytest.fixture
def user(company):
return User.objects.create(
email="test@example.com",
company=company
)
Best Practices
- Test Coverage: Aim for high test coverage
- Isolated Tests: Tests should be independent
- Clear Names: Use descriptive test names
- Setup/Teardown: Properly set up and clean up
- Mock External Services: Mock external API calls
Next Steps
- Contributing - Contribution guidelines
- Models Guide - Data models
- Services Guide - Service layer
For testing questions, refer to pytest documentation or contact the development team.