This document describes the testing framework and code quality tools set up for the Fin Officer project.
We've implemented a comprehensive testing and code quality framework that includes:
- Unit Tests: Using pytest for testing individual components
- Code Formatting: Using Black to ensure consistent code style
- Linting: Using Flake8 and Pylint to catch potential issues
- Import Sorting: Using isort to organize imports
- Continuous Integration: Using GitHub Actions to automate testing
Tox is configured to run tests in isolated environments. To run all tests and checks:
toxTo run only the tests:
tox -e py310To run only the linting checks:
tox -e lintTo run only the formatting checks:
tox -e formatTo run tests directly with pytest:
python -m pytest tests/To run a specific test file:
python -m pytest tests/test_mcp_auto_reply_unit.pyTo format your code with Black:
black app/ tests/To sort imports with isort:
isort app/ tests/To check your code with Flake8:
flake8 app/ tests/To check your code with Pylint:
pylint app/GitHub Actions is configured to run tests and code quality checks on every push to the main branch and on pull requests. The workflow is defined in .github/workflows/python-tests.yml.
To generate a test coverage report:
python -m pytest --cov=app tests/Unit tests should be placed in the tests/ directory and should follow the naming convention test_*.py. Each test function should be prefixed with test_.
For asynchronous tests, use the @pytest.mark.asyncio decorator.
Example:
import pytest
@pytest.mark.asyncio
async def test_generate_auto_reply():
# Test code here
passUse the unittest.mock module for mocking dependencies in tests.
Example:
from unittest.mock import patch, AsyncMock
@pytest.mark.asyncio
async def test_with_mocks():
with patch('module.function', return_value=AsyncMock()):
# Test code here
passTest data should be defined in fixtures to make it reusable across tests.
Example:
@pytest.fixture
def sample_email():
return EmailSchema(
id=1,
from_email="test@example.com",
to_email="support@finofficer.com",
subject="Test Subject",
content="Test Content",
received_date="2025-05-20T00:00:00"
)- Write tests before implementing features (Test-Driven Development)
- Keep tests small and focused on a single functionality
- Use descriptive test names that explain what is being tested
- Use fixtures for common setup code
- Mock external dependencies to isolate the code being tested
- Run tests frequently during development
- Maintain high test coverage for critical components