Overview
Theanchor test command builds programs, starts a local validator, deploys programs, and runs your test suite.
Command Syntax
Alias
Options
Test Configuration
Build and test only this program
Skip deploying programs (test against previously deployed programs)
Skip building the program in the workspace
Skip checking for safety comments (“CHECK”) in the code
Do not build the IDL
Validator Options
Skip starting a local validator (if configured cluster is localnet)
Validator type to use for local testingOptions:
surfpool: Use Surfpool validator (default, faster)legacy: Use Solana test validator
Keep the local validator running after tests complete
Build Options
Architecture to use when building the programOptions:
sbf, bpfEnvironment variables to pass into the Docker container
Arguments to pass to the underlying
cargo build-sbf command (use -- to separate)Test Suite Options
Run test suites under the specified path(s)
Additional arguments to pass to the test runner
Examples
Basic Test
Run all tests with automatic build and deployment:Test Specific Program
Skip Build (Faster Testing)
Skip building when the program code hasn’t changed:Test Against Deployed Programs
Test without redeploying programs:Keep Validator Running
Useful for inspecting transactions after tests:Run Specific Test Files
Use Legacy Validator
Pass Arguments to Test Runner
Complete Skip (No Build, No Deploy)
Test Workflow
The test command executes the following steps:-
Build Phase (unless
--skip-build):- Compiles Rust programs
- Generates IDL files
- Runs build hooks
-
Validator Phase (unless
--skip-local-validator):- Starts local validator (Surfpool or legacy)
- Waits for validator to be ready
-
Deploy Phase (unless
--skip-deploy):- Deploys all programs to the local validator
- Uploads IDL for each program
-
Test Phase:
- Runs the test script defined in
Anchor.toml - Executes test suites (Mocha, Jest, etc.)
- Runs the test script defined in
-
Cleanup Phase:
- Stops the validator (unless
--detach)
- Stops the validator (unless
Validator Types
Surfpool (Default)
- Faster startup time
- Optimized for Anchor development
- Managed automatically by Anchor
Legacy
- Standard
solana-test-validator - Compatible with all Solana CLI features
- Useful for advanced testing scenarios
Test Configuration
The test script is defined inAnchor.toml:
Environment Setup
Tests have access to:anchor.workspace.<ProgramName>: Program clientsanchor.AnchorProvider.env(): Provider connected to local validatoranchor.web3: Solana web3.js library
Example Test (TypeScript)
Performance Tips
Notes
When testing on devnet or mainnet (not localnet), the validator is not started automatically.
The default timeout for Mocha tests is 1000000ms (1000 seconds) to accommodate Solana’s transaction confirmation times.