This directory contains the test infrastructure for the URST (MicroPython Reliable Serial Transport) project.
The test infrastructure provides:
- Base test class with comprehensive assertion methods
- Test runner with individual file execution with reporting
- Coverage tracking capabilities to measure test effectiveness
- Mock helpers for test data generation and error simulation
base_test.py- Base test class with common assertion methods and utilitiestestrunner.py- Test runner with reporting; runs all or specific testscoverage_tracker.py- Coverage tracking utility for test effectiveness measurement_mocks.py- Mock infrastructure for MicroPython compatibility_colors.py- ANSI color constants used for consistent runner output
test_base_infrastructure.py- Tests for the base test infrastructure itselftest_codec.py- Tests forurst/codec.pytest_handler.py- Tests forurst/handler.pytest_protocol.py- Tests forurst/protocol.pytest_transport.py- Tests forurst/transport.py
# From device/tests
python testrunner.py# Quiet output
python testrunner.py --quiet
# List available test files
python testrunner.py --list
# Verbose diagnostics (if supported)
python testrunner.py --verbose# Run a specific test file
python testrunner.py test_protocol.py
# Run multiple files
python testrunner.py test_codec.py test_transport.py
# Short names are supported (auto-adds test_ prefix and .py)
python testrunner.py protocol transport# Run individual test file directly
python test_base_infrastructure.py
python test_codec.py
python test_handler.py
python test_protocol.py
python test_transport.pyThe BaseTest class provides a comprehensive set of assertion methods:
assert_equal(actual, expected, message)- Assert equalityassert_not_equal(actual, expected, message)- Assert inequalityassert_true(condition, message)- Assert condition is trueassert_false(condition, message)- Assert condition is falseassert_none(value, message)- Assert value is Noneassert_not_none(value, message)- Assert value is not None
assert_in(item, container, message)- Assert item is in containerassert_not_in(item, container, message)- Assert item is not in container
assert_bytes_equal(actual, expected, message)- Assert bytes equality with hex outputassert_raises(exception_type, callable, *args, **kwargs)- Assert exception is raised
setup()- Override for test setup (called before tests)teardown()- Override for test cleanup (called after tests)run_all_tests()- Execute all test methods and return results
-
Import the base class:
from _base_test import BaseTest, MockTestHelper
-
Create test class:
class TestMyComponent(BaseTest): def __init__(self): super().__init__("MyComponent")
-
Add test methods:
def test_basic_functionality(self): """Test basic functionality.""" result = my_function() self.assert_equal(result, expected_value, "Function returns expected value")
-
Add main execution:
if __name__ == "__main__": test_suite = TestMyComponent() results = test_suite.run_all_tests() sys.exit(0 if results['failed'] == 0 else 1)
The MockTestHelper class provides utilities for test data generation:
create_test_data(size, pattern=0x55)- Create test data with patterncreate_random_data(size, seed=42)- Create pseudo-random test datacorrupt_data(data, position, new_value=0xFF)- Corrupt data for error testing
The coverage tracker provides simple coverage analysis:
from _coverage_tracker import create_coverage_tracker
# Create tracker
tracker = create_coverage_tracker("../urst")
tracker.analyze_source_files()
tracker.start_tracking()
# Run tests...
tracker.stop_tracking()
tracker.print_coverage_report(detailed=True)The test runner provides detailed output:
================================================================================
Running 2 test files...
================================================================================
--- Running test: test_example.py ---
PASS: Basic functionality works
FAIL: Edge case handling
Expected: True
Actual: False
--- Finished test: test_example.py ---
================================================================================
TEST SUMMARY
================================================================================
Files: 1/2 passed
Tests: 15/16 passed
Duration: 0.123s
Overall Success Rate: 93.8%
================================================================================
- Use descriptive test method names starting with
test_ - Include meaningful assertion messages to help with debugging
- Test normal cases, edge cases, and error conditions
- Use setup/teardown for resource management
- Keep tests independent - each test should work in isolation
- Use mock helpers for consistent test data generation
The test infrastructure is designed to work with both:
- CPython (development environment)
- MicroPython (target environment)
Mock objects are used to simulate MicroPython-specific modules when running on CPython.