Testing Strategies
There are three main approaches to testing Anchor programs:- Integration Tests - Test your program by deploying it to a local validator and interacting with it through a client
- Unit Tests - Test individual functions and components in isolation using specialized test frameworks
- Rust Tests - Write tests directly in Rust using the Anchor Rust client or test frameworks
The anchor test Command
The anchor test command is the primary way to run integration tests for your Anchor programs. It automates the entire testing workflow:
anchor test on a localnet cluster, it will:
- Start a local Solana validator (
solana-test-validator) - Build your programs using
anchor build - Deploy your programs to the local validator
- Run the test files in the
tests/directory - Stop the local validator
Common Options
Manual Testing Workflow
For more control during development, you can manually run each step:- Keep the validator running between test runs
- Inspect accounts on the Solana Explorer
- Debug transaction logs in real-time
- Iterate quickly without restarting the validator
TypeScript Integration Tests
The default test template uses TypeScript with the Anchor client library. Tests are located in thetests/ directory.
Basic Test Structure
Here’s a simple test example:Advanced Test Example
Here’s a more complex example testing an escrow program:Rust Tests
You can write tests in Rust using the Anchor Rust client. To initialize a project with Rust tests:tests/src/:
Test Configuration
Test behavior is configured inAnchor.toml:
Program Logs
When running tests, program logs are automatically streamed to:msg!() in your program code to add debug logging:
Specialized Testing Frameworks
For more advanced testing scenarios, Anchor supports specialized testing frameworks:- [LiteSVMtesting/litesvm) - Fast and lightweight in-process testing in Rust, TypeScript, and Python
- [Mollusktesting/mollusk) - Lightweight Rust testing harness for direct SVM program execution
- Faster test execution (no validator startup time)
- More control over the test environment
- Advanced features like time travel and arbitrary account manipulation
- Better isolation for unit testing
Best Practices
- Write tests early - Use Test-Driven Development (TDD) to write tests before implementing features
- Test edge cases - Include tests for error conditions, boundary values, and invalid inputs
- Use descriptive test names - Make it clear what each test is verifying
- Keep tests isolated - Each test should be independent and not rely on other tests
- Mock external dependencies - Use test accounts and fixtures instead of live accounts when possible
- Test on devnet before mainnet - Always test on devnet before deploying to mainnet
- Use continuous integration - Automate testing with CI/CD pipelines
Next Steps
- Learn about [LiteSVMtesting/litesvm) for fast in-process testing
- Explore [Mollusktesting/mollusk) for lightweight Rust testing
- Read about the TypeScript Client
- Check out the Rust Client documentation