Testing Guide
This guide covers how to run tests, understand the test structure, and write new tests for the DZDK CLI.Overview
The DZDK CLI uses pytest as its testing framework, with additional plugins for coverage reporting and mocking.Testing Stack
- pytest 8.0.0 - Main testing framework
- pytest-cov 4.1.0 - Code coverage reporting
- pytest-mock 3.12.0 - Mocking and patching utilities
- Click Testing - Built-in CLI testing utilities from the Click framework
Running Tests
Prerequisites
Ensure you have installed development dependencies:Basic Test Execution
Run all tests:Running Specific Tests
Run a specific test file:Code Coverage
Run tests with coverage report:htmlcov/ directory. Open htmlcov/index.html in your browser to view detailed coverage.
Generate coverage report with missing lines:
Test Output Options
Show print statements during test execution:Test Structure
Tests are located in thetests/ directory:
Test File Organization
Thetest_cli.py file contains tests for all CLI commands, organized by functionality:
- Fixtures - Reusable test components
- Command Tests - Tests for each CLI command
- Error Handling Tests - Tests for error scenarios
- Integration Tests - End-to-end workflow tests
Test Fixtures
Fixtures provide reusable test components. Here are the main fixtures used:runner Fixture
Provides a Click CLI test runner:
mock_config_file Fixture
Creates a temporary configuration file:
mock_env Fixture
Sets up test environment variables:
mock_requests Fixture
Mocks HTTP requests to the API:
Writing Tests
Basic Test Structure
Follow this structure for writing tests:Testing CLI Commands
Example: Testing a List Command
Example: Testing with Options
Example: Testing Configuration
Testing File Operations
Usetmp_path fixture for file operations:
Testing Error Handling
Test various error scenarios:Testing HTTP Errors
Testing Validation
Mocking API Responses
Usepatch to mock API responses:
Test Best Practices
1. Test Names
- Use descriptive names starting with
test_ - Name should describe what is being tested
- Examples:
test_health_commandtest_services_list_with_paginationtest_photo_upload_file_not_found
2. Test Documentation
- Include docstrings explaining the test purpose
- Document any non-obvious setup or assertions
3. Arrange-Act-Assert Pattern
Organize tests using the AAA pattern:4. Test Independence
- Each test should be independent
- Don’t rely on test execution order
- Use fixtures for shared setup
- Clean up any created resources
5. Mock External Dependencies
- Always mock HTTP requests to APIs
- Mock file system operations when appropriate
- Use fixtures for consistent mocking
6. Test Edge Cases
- Test both success and failure scenarios
- Test boundary conditions (empty lists, max values, etc.)
- Test invalid inputs and error handling
7. Keep Tests Fast
- Use mocks to avoid actual API calls
- Don’t include unnecessary
sleep()calls - Run expensive tests separately if needed
Common Testing Patterns
Pattern: Testing Interactive Prompts
Pattern: Testing Progress Indicators
Pattern: Testing Configuration Loading
Debugging Tests
Print Debug Information
-s flag to see print output:
Use PDB Debugger
Check Exception Details
Continuous Integration
Tests run automatically on:- Pull request creation
- Commits to main branch
- Manual workflow dispatch
Test Coverage Goals
- Minimum coverage: 80%
- Target coverage: 90%+
- Focus on critical paths and error handling
- Don’t sacrifice test quality for coverage numbers
Need Help?
If you need help with testing:- Check existing tests in
tests/test_cli.pyfor examples - Review the pytest documentation
- See the Contributing Guide for getting started
- Open an issue on GitHub for specific questions