-
Notifications
You must be signed in to change notification settings - Fork 8
Testing
Max Azatian edited this page Oct 5, 2024
·
1 revision
Tests are organized into two main categories:
- Unit Tests: Located in
chat_service/app/tests/unit/ - Integration Tests: Located in
chat_service/app/tests/integration/
Unit tests focus on testing individual components in isolation. Key unit test files include:
-
test_database.py: Tests database connection and operations -
test_event_dispatcher.py: Verifies event dispatching mechanism -
test_event_handlers.py: Checks event handler functionality -
test_models.py: Ensures ORM models are correctly defined -
test_redis_client.py: Tests Redis client operations -
test_security.py: Verifies security-related utilities (e.g., password hashing) -
test_unit_of_work.py: Tests the Unit of Work pattern implementation
Integration tests verify the interaction between different components and the API endpoints. Key integration test files include:
-
test_auth.py: Tests authentication-related endpoints -
test_chats.py: Verifies chat-related API functionality -
test_messages.py: Checks message-related API endpoints -
test_users.py: Tests user-related API operations
-
conftest.py: Contains pytest fixtures and configuration for the test suite -
pytest.ini: Pytest configuration file
To run the backend tests, navigate to the chat_service directory and execute:
pytestThis will run all unit and integration tests. To run specific test categories:
- For unit tests only:
pytest app/tests/unit - For integration tests only:
pytest app/tests/integration
GitHub Actions are used for continuous integration. The workflow is defined in .github/workflows/tests.yml. This workflow:
- Sets up the Python environment
- Installs dependencies
- Runs the test suite
- Reports test coverage
I use pytest-cov to measure test coverage. To generate a coverage report:
pytest --cov=app --cov-report=htmlThis command will create an HTML coverage report in the htmlcov directory.
When adding new features or modifying existing ones:
- Always add or update corresponding unit tests
- For API changes, update or add new integration tests
- Aim for high test coverage, especially for critical paths
- Use pytest fixtures to set up test data and dependencies
- Mock external services and dependencies in unit tests