Test Organization
Tests are organized into different categories:Running Tests
Unit Tests
Unit tests are fast and run automatically in CI. They test individual components in isolation.In-Memory Integration Tests
These integration tests use in-memory simulation and don’t require Google OAuth credentials. They run in CI.Google Drive Integration Tests
These tests require Google OAuth credentials and test the full federated learning workflow using Google Drive as the transport layer. They are marked asslow and must be run manually.
All Tests
Run the complete test suite including unit and integration tests:Test Markers
Tests use pytest markers to categorize and filter test execution:slow: Integration tests with Google Drive (excluded from CI)
Running Tests by Marker
Writing Tests
Test Structure
Follow these conventions when writing tests:Best Practices
Unit Tests
Unit Tests
- Test one thing at a time
- Use descriptive test names that explain what is being tested
- Keep tests fast and isolated
- Mock external dependencies
- Use fixtures for common test data
Integration Tests
Integration Tests
- Mark slow tests with
@pytest.mark.slow - Test real-world scenarios and workflows
- Clean up resources after tests
- Document any required setup or credentials
Async Tests
Async Tests
- Use
@pytest.mark.asynciofor async tests - Ensure proper cleanup of async resources
- Use
pytest-asynciofixtures when needed
CI/CD Testing
Automated Tests (GitHub Actions)
Unit and in-memory integration tests run automatically on every push/PR to main branch:- Operating Systems: Ubuntu, Windows, macOS
- Python Versions: 3.10, 3.11, 3.12
Manual Integration Tests
Google Drive integration tests can be triggered manually via GitHub Actions. Required secrets must be configured in repository settings:| Secret Name | Content |
|---|---|
GDRIVE_CRED_DO1 | Contents of do1.json |
GDRIVE_CRED_DO2 | Contents of do2.json |
GDRIVE_CRED_DS | Contents of ds.json |
GDRIVE_TOKEN_DO1 | Contents of token_do1.json |
GDRIVE_TOKEN_DO2 | Contents of token_do2.json |
GDRIVE_TOKEN_DS | Contents of token_ds.json |
EMAIL_DO1 | DO1 email address |
EMAIL_DO2 | DO2 email address |
EMAIL_DS | DS email address |
Test Configuration
pytest Configuration
The project’spyproject.toml includes pytest configuration:
Parallel Test Execution
Unit tests usepytest-xdist for parallel execution to speed up test runs:
Coverage Reports
Generate code coverage reports to identify untested code:Troubleshooting
Tests fail with import errors
Tests fail with import errors
Ensure you’ve installed the package in editable mode:
Google Drive tests fail with OAuth errors
Google Drive tests fail with OAuth errors
- Verify
credentials/.envis configured correctly - Check that OAuth credentials files exist in
credentials/ - Ensure the OAuth app is published (not in testing mode)
- Re-authenticate by deleting token files and running tests again
Tests hang or timeout
Tests hang or timeout
- Check for deadlocks in async code
- Increase timeout values for slow operations
- Ensure proper cleanup of resources
- Run tests with verbose output:
pytest -v -s
Parallel tests fail but sequential tests pass
Parallel tests fail but sequential tests pass
- Check for shared state between tests
- Use fixtures to isolate test data
- Avoid using global variables
- Run with
-n 1to disable parallelization for debugging
Next Steps
- Learn about Code Quality tools and standards
- Understand the Release Process
- Review Versioning strategy