OC-201b · Module 3
Testing Custom Modules
3 min read
A module without tests is a module you are afraid to change. And a module you are afraid to change is a module that accumulates technical debt with every requirement update. Testing OpenClaw modules follows the same 3-layer principle as the architecture itself. Test the data layer independently — does the API wrapper return the expected format? Test the logic layer independently — does the transformation produce correct business results from known inputs? Test the presentation layer independently — does the formatted output match the channel requirements?
The critical testing pattern for OpenClaw modules is mock-at-the-boundary. Replace external dependencies — APIs, databases, the AI model itself — with deterministic mocks that return known data. Then test your module logic against that known data. This eliminates the variability that makes AI-dependent code hard to test. Your data layer mock returns the same weather data every time. Your logic layer processes it the same way every time. Your tests are reliable, fast, and catch regressions immediately.
- 1. Mock External Dependencies Create fixtures that return known, deterministic data for every API and database your module touches. The fixtures should include success cases, error cases, and edge cases (empty results, rate limits, malformed responses).
- 2. Test the Data Layer Verify that the API wrapper correctly handles authentication, retries, rate limiting, and response normalization. Use the mock API to simulate each scenario. The data layer test should never make a real network call.
- 3. Test the Logic Layer Feed known data into the logic layer and verify the business results. Test the happy path, edge cases, and error propagation. The logic layer test should never call the data layer — inject mock data directly.
- 4. Test the Composition After testing each layer independently, run an integration test with all layers connected but external dependencies still mocked. This verifies that the layers integrate correctly without depending on external availability.