Testing Guide for aiogram-cmds¶
Overview¶
The aiogram-cmds library includes a comprehensive test suite with 236 tests achieving 97% code coverage. The test suite is organized into three categories:
- Unit Tests (212 tests): Test individual components in isolation
- Integration Tests (14 tests): Test complete workflows and component interactions
- Performance Tests (10 tests): Test performance characteristics and scalability
Test Structure¶
tests/
├── unit/ # Unit tests (212 tests)
│ ├── test_apply.py # Configuration application tests (27 tests)
│ ├── test_auto_setup.py # Auto-setup functionality tests (28 tests)
│ ├── test_builder.py # Command builder tests (20 tests)
│ ├── test_manager.py # Manager tests (31 tests)
│ ├── test_policy.py # Policy tests (24 tests)
│ ├── test_registry.py # Registry tests (15 tests)
│ ├── test_setters.py # Setters tests (26 tests)
│ ├── test_settings.py # Settings tests (24 tests)
│ └── test_translator.py # Translator tests (17 tests)
├── integration/ # Integration tests (14 tests)
│ ├── conftest.py # Shared integration fixtures
│ └── test_full_workflow.py # Complete workflow tests
└── perf/ # Performance tests (10 tests)
├── conftest.py # Performance test fixtures
└── test_benchmarks.py # Performance benchmarks
Running Tests¶
Run All Tests¶
Run Specific Test Categories¶
# Unit tests only
uv run pytest tests/unit/
# Integration tests only
uv run pytest tests/integration/
# Performance tests only
uv run pytest tests/perf/
Run with Coverage¶
Run Specific Test Files¶
Test Categories¶
Unit Tests¶
Unit tests verify individual components work correctly in isolation:
test_apply.py: Tests configuration application, scope creation, and command setuptest_auto_setup.py: Tests auto-setup convenience functions and default configurationstest_builder.py: Tests command building, name validation, and i18n integrationtest_manager.py: Tests the main CommandScopeManager class and its methodstest_policy.py: Tests command policies, flags, and profile resolutiontest_registry.py: Tests command registry and profile resolution logictest_setters.py: Tests command setting, clearing, and scope managementtest_settings.py: Tests settings loading, validation, and configurationtest_translator.py: Tests translation functionality and i18n integration
Integration Tests¶
Integration tests verify complete workflows and component interactions:
- Full Workflow Tests: Test complete setup-to-usage workflows
- Multi-Language Integration: Test multi-language support across components
- Profile System Integration: Test complex profile hierarchies and resolution
- Scope Hierarchy Integration: Test scope precedence and application order
- Real-World Scenarios: Test e-commerce and support bot scenarios
- Error Handling Integration: Test error handling across component boundaries
Performance Tests¶
Performance tests measure scalability and efficiency:
- Setup Performance: Test setup time with various configuration sizes
- User Update Performance: Test user command update performance
- Profile Resolution Performance: Test profile resolution speed
- Memory Usage Scaling: Test memory usage with large configurations
- Concurrent Operations: Test performance under concurrent load
- Large-Scale Integration: Test performance with realistic large-scale usage
- Scalability Limits: Test behavior at Telegram API limits
Coverage Report¶
Current test coverage: 97%
| Module | Coverage | Status |
|---|---|---|
__init__.py |
100% | ✅ Perfect |
apply.py |
100% | ✅ Perfect |
auto_setup.py |
100% | ✅ Perfect |
translator.py |
100% | ✅ Perfect |
registry.py |
100% | ✅ Perfect |
setters.py |
100% | ✅ Perfect |
customize.py |
100% | ✅ Perfect |
dynamic.py |
100% | ✅ Perfect |
policy.py |
100% | ✅ Perfect |
manager.py |
98% | ✅ Excellent |
builder.py |
97% | ✅ Excellent |
settings.py |
97% | ✅ Excellent |
builders_configured.py |
83% | ✅ Good |
version.py |
0% | ⚠️ Minor (just version string) |
Test Fixtures¶
Unit Test Fixtures¶
mock_bot: Mock Bot instance for testingsample_config: Sample CmdsConfig for testingsample_settings: Sample CmdsSettings for testing
Integration Test Fixtures¶
mock_bot: AsyncMock Bot instancemock_i18n: Mock i18n instance with translation supportsample_config: Comprehensive sample configurationsample_settings: Sample settings configuration
Performance Test Fixtures¶
mock_bot: Fast mock Bot instancelarge_config: Large configuration for performance testing
Writing New Tests¶
Unit Test Guidelines¶
- Test one component at a time
- Use mocks for external dependencies
- Test both success and error cases
- Include edge cases and boundary conditions
- Use descriptive test names
Example:
def test_command_builder_with_translator(self, mock_bot):
"""Test command building with translator integration."""
mock_translator = Mock()
mock_translator.return_value = "Translated command"
commands = build_bot_commands(
["start"],
lang="en",
translator=mock_translator
)
assert len(commands) == 1
assert commands[0].command == "start"
mock_translator.assert_called()
Integration Test Guidelines¶
- Test complete workflows
- Use realistic scenarios
- Test component interactions
- Verify end-to-end functionality
Example:
@pytest.mark.asyncio
async def test_full_setup_workflow(self, mock_bot):
"""Test complete setup workflow."""
manager = CommandScopeManager(bot=mock_bot)
await manager.setup_all()
await manager.update_user_commands(
user_id=12345,
is_registered=True,
has_vehicle=False,
is_during_registration=False,
)
assert mock_bot.set_my_commands.call_count >= 1
Performance Test Guidelines¶
- Measure actual performance metrics
- Test with realistic data sizes
- Verify scalability characteristics
- Set reasonable performance expectations
Example:
@pytest.mark.asyncio
async def test_setup_performance(self, mock_bot):
"""Test setup performance with large config."""
config = create_large_config()
start_time = time.time()
manager = CommandScopeManager(bot=mock_bot, config=config)
await manager.setup_all()
setup_time = time.time() - start_time
assert setup_time < 5.0 # Should complete in reasonable time
Continuous Integration¶
Tests run automatically on: - Pull Requests: All tests must pass - Main Branch: All tests must pass - Releases: All tests must pass with 97%+ coverage
Test Maintenance¶
- Regular Updates: Tests are updated with new features
- Coverage Monitoring: Maintain 97%+ coverage
- Performance Monitoring: Ensure performance tests pass
- Documentation: Keep test documentation current
Debugging Tests¶
Common Issues¶
- Async Test Failures: Ensure
@pytest.mark.asynciois used - Mock Issues: Verify mock setup and assertions
- Import Errors: Check test imports and module paths
- Coverage Gaps: Add tests for uncovered code paths
Debug Commands¶
# Run with verbose output
uv run pytest tests/ -v
# Run specific test with debugging
uv run pytest tests/unit/test_manager.py::TestCommandScopeManager::test_basic_setup -v -s
# Run with coverage and show missing lines
uv run pytest tests/ --cov=src/aiogram_cmds --cov-report=term-missing
Best Practices¶
- Test Coverage: Maintain high test coverage (97%+)
- Test Quality: Write meaningful, maintainable tests
- Performance: Ensure tests run quickly
- Documentation: Document test purpose and scenarios
- CI/CD: Integrate tests into development workflow
The comprehensive test suite ensures aiogram-cmds is reliable, maintainable, and ready for production use.