Skip to main content

Testing

One of Contract’s biggest advantages is testability. Since your services are just plain Go structs with methods, you can test them directly without HTTP mocking, framework setup, or complex test fixtures.

Why Contract Services Are Easy to Test

Traditional API testing looks like this:
With Contract:

Unit Testing Your Service

Unit tests test your business logic directly, without any transport layer.

Basic Test

Table-Driven Tests

For testing multiple scenarios, use table-driven tests:

Testing Error Types

When using Contract’s error types, verify the error code:

Testing Services with Dependencies

Real services have dependencies like databases and caches. Use dependency injection and interfaces for testability.

Define Interfaces

Create Mock Implementations

Test with Mocks

Integration Testing Transports

Sometimes you need to test the full HTTP flow. Contract makes this easy too.

Testing REST Endpoints

Testing JSON-RPC

Testing MCP

Test Helpers

Create helper functions to reduce boilerplate:

Best Practices

1. Test Business Logic Separately from Transports

2. Use Subtests for Organization

3. Test Error Conditions

Don’t just test the happy path:

4. Use Parallel Tests When Safe

See Also