Running SDK Tests
Relevant source files
The following files were used as context for generating this wiki page:
The Anthropic Go SDK utilizes a comprehensive testing suite to ensure API compatibility, correct serialization, and robust handling of various features like streaming, tool use, and AWS Bedrock integrations. Tests range from unit tests of internal logic to integration tests that simulate interaction with the Anthropic API using mock servers.
Most tests are designed to run against a mock server that adheres to the OpenAPI specification, ensuring that the SDK correctly handles the expected request and response formats defined by the Claude platform.
Sources: CONTRIBUTING.md:46-48, usage_test.go:19-22
Test Environment Setup
Before running tests, the environment must be properly initialized. The project provides several scripts to automate dependency installation and the setup of the mock environment.
Scripts and Commands
| Script | Purpose |
|---|---|
./scripts/bootstrap | Installs required dependencies and builds the SDK. |
./scripts/lint | Runs linting tools to ensure code quality. |
./scripts/mock | Sets up a mock server against the OpenAPI spec. |
./scripts/test | Executes the Go test suite. |
Sources: CONTRIBUTING.md:10-15, CONTRIBUTING.md:46-52
Mock Server Configuration
Tests typically check for a local mock server running at http://localhost:4010. This can be overridden using environment variables to point to a different test endpoint.
The diagram above shows how the SDK tests resolve the target API endpoint for testing purposes.
Sources: usage_test.go:18-25, betamessage_test.go:17-24
Core Test Categories
Integration and Usage Tests
These tests verify high-level client functionality. They often use testutil.CheckTestServer to verify the availability of the mock environment before initializing a client with a dummy API key.
Key components tested:
- Message Creation: Verifying
client.Messages.Newwith various parameters. - Pagination: Testing manual and automatic paging of list results (e.g., Message Batches).
- Beta Features: Specialized tests for features like
Beta.Messages.Batches.
Sources: usage_test.go:26-30, paginationmanual_test.go:18-35, betamessagebatch_test.go:19-35
AWS Bedrock and Authentication Tests
The SDK includes specific tests for AWS-related functionality located in the aws/ directory. These tests cover SigV4 signing, header injection, and configuration resolution.
This diagram illustrates the flow of an AWS-specific test, including the local mock server setup.
Sources: aws/aws_test.go:48-61, aws/aws_test.go:167-185
Tool Runner and Logic Tests
Tests in toolrunner/runner_test.go focus on the conversation loop where Claude calls tools. These tests verify:
- Concurrency: Ensuring multiple tool calls are executed in parallel.
- Error Recovery: Surfacing tool execution errors back to the model as
is_error: true. - Context Cancellation: Verifying that cancelling a context stops ongoing tool execution.
Sources: toolrunner/runner_test.go:197-210, toolrunner/runner_test.go:276-290
Implementation Details
VCR Mocking
For complex flows like the tool runner, the SDK uses a VCR-style HTTP client to record and replay API interactions, ensuring consistent test results without requiring a live backend.
Sources: toolrunner/runner_test.go:68-72, toolrunner/runner_test.go:107-110
Parameter Validation
Tests also verify that the SDK correctly enforces requirements, such as missing AWS Workspace IDs or incompatible configuration options.
Sources: aws/aws_test.go:87-100
Streaming Accumulation
The TestBetaAccumulate suite verifies the logic that combines multiple Server-Sent Event (SSE) chunks into a final message object, covering text blocks, tool use blocks, and thinking blocks.
Sources: betamessage_test.go:226-250
Summary
The SDK testing strategy relies on a combination of OpenAPI-driven mock servers and local httptest servers. By utilizing scripts for environment setup and specialized test suites for AWS and Tool Runners, the SDK ensures that core features like SigV4 signing, streaming accumulation, and parallel tool execution function correctly before deployment.
Sources: CONTRIBUTING.md:46-52, aws/aws_test.go:48-52, toolrunner/runner_test.go:197-205