Skip to main content
OpenShift Python Wrapper uses pytest for testing with a comprehensive fake Kubernetes client that allows testing without a real cluster.

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

Coverage reports are generated in:
  • .tests_coverage/ (HTML format)
  • Terminal output

Run Specific Test Class or Method

Test Structure

Basic Test Pattern

Tests use the fake client fixture provided in conftest.py:

Incremental Tests

Tests marked with @pytest.mark.incremental will stop if a test fails:
This is useful for CRUD tests where later tests depend on earlier ones.

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 in tests/test_resources/ with the naming pattern:
  • Podtest_pod.py
  • ConfigMaptest_config_map.py
  • VirtualMachinetest_virtual_machine.py

Test Fixtures

Common Fixtures

Defined in tests/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 class
  • scope="module": One instance shared across test file
  • scope="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

  1. Use incremental tests for CRUD operations
  2. Use descriptive test names that explain what’s being tested
  3. Test one thing per test - keep tests focused
  4. Use the fake client for unit tests
  5. Clean up resources after tests (use clean_up() or context managers)
  6. Add docstrings to test methods
  7. Number incremental tests (test_01, test_02, etc.) for clarity
  8. Use fixtures to share setup code
  9. Test edge cases and error conditions
  10. 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.py is in the test directory or parent
  • Fixture is defined with @pytest.fixture
  • Fixture name matches parameter name

Additional Resources