Skip to main content
Testing is a critical part of Solana program development. Anchor provides built-in support for testing your programs using TypeScript, Rust, and specialized testing frameworks. This section covers the testing strategies and tools available for Anchor programs.

Testing Strategies

There are three main approaches to testing Anchor programs:
  1. Integration Tests - Test your program by deploying it to a local validator and interacting with it through a client
  2. Unit Tests - Test individual functions and components in isolation using specialized test frameworks
  3. 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:
When you run anchor test on a localnet cluster, it will:
  1. Start a local Solana validator (solana-test-validator)
  2. Build your programs using anchor build
  3. Deploy your programs to the local validator
  4. Run the test files in the tests/ directory
  5. Stop the local validator

Common Options

Manual Testing Workflow

For more control during development, you can manually run each step:
This approach is useful when you want to:
  • 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 the tests/ 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:
Rust test files are located in tests/src/:

Test Configuration

Test behavior is configured in Anchor.toml:

Program Logs

When running tests, program logs are automatically streamed to:
You can use 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
These frameworks offer:
  • 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

  1. Write tests early - Use Test-Driven Development (TDD) to write tests before implementing features
  2. Test edge cases - Include tests for error conditions, boundary values, and invalid inputs
  3. Use descriptive test names - Make it clear what each test is verifying
  4. Keep tests isolated - Each test should be independent and not rely on other tests
  5. Mock external dependencies - Use test accounts and fixtures instead of live accounts when possible
  6. Test on devnet before mainnet - Always test on devnet before deploying to mainnet
  7. 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