Test Framework Overview
The project uses:- pytest: Test framework
- Fake Kubernetes Client: In-memory Kubernetes API simulation
- Coverage: Code coverage reporting (minimum 65%)
- Incremental tests: Tests that depend on previous test results
Running Tests
Run All Tests
Run Specific Test File
Run Tests by Pattern
Run with Coverage
.tests_coverage/(HTML format)- Terminal output
Run Specific Test Class or Method
Test Structure
Basic Test Pattern
Tests use the fake client fixture provided inconftest.py:
Incremental Tests
Tests marked with@pytest.mark.incremental will stop if a test fails:
Fake Kubernetes Client
The fake client simulates Kubernetes API behavior without requiring a real cluster.Using the Fake Client
Fake Client Features
- In-memory resource storage
- Simulates resource CRUD operations
- Supports annotations for controlling behavior
- No cluster connection required
- Fast test execution
Controlling Resource State
Use annotations to control resource behavior:Generating Tests
Automated Test Generation
Generate tests for newly added resources using the test generator:Using Class Generator
The class generator can also add tests:Tests are only generated for classes that were generated by the class-generator.
Generated Test Location
Tests are created intests/test_resources/ with the naming pattern:
Pod→test_pod.pyConfigMap→test_config_map.pyVirtualMachine→test_virtual_machine.py
Test Fixtures
Common Fixtures
Defined intests/conftest.py:
Custom Fixtures
Create resource-specific fixtures:Fixture Scopes
scope="function": New fixture instance for each test (default)scope="class": One instance shared across test classscope="module": One instance shared across test filescope="session": One instance for entire test session
Testing Patterns
Testing Resource Creation
Testing Resource Retrieval
Testing Resource Updates
Testing Resource Deletion
Testing Resource Conditions
Testing Resource Events
Coverage Requirements
Minimum Coverage
The project requires minimum 65% code coverage:pyproject.toml
Excluded from Coverage
pyproject.toml
Test Markers
Available Markers
pyproject.toml
Using Markers
Running Marked Tests
Debugging Tests
Verbose Output
Show Print Statements
Drop into Debugger on Failure
Run Failed Tests
Best Practices
- Use incremental tests for CRUD operations
- Use descriptive test names that explain what’s being tested
- Test one thing per test - keep tests focused
- Use the fake client for unit tests
- Clean up resources after tests (use
clean_up()or context managers) - Add docstrings to test methods
- Number incremental tests (test_01, test_02, etc.) for clarity
- Use fixtures to share setup code
- Test edge cases and error conditions
- Maintain coverage above 65%
CI/CD Integration
Tests run automatically in CI/CD pipelines:- On every pull request
- Before merging to main
- Coverage reports are generated
- All checks must pass before merge
Troubleshooting
Tests Not Found
Ensure test files:- Start with
test_ - Are in a directory with
__init__.py - Contain classes starting with
Test - Contain methods starting with
test_
Import Errors
Make sure you’re running tests with the correct command:Fixture Not Found
Check that:conftest.pyis in the test directory or parent- Fixture is defined with
@pytest.fixture - Fixture name matches parameter name