litesvm-testing
A comprehensive testing and benchmarking framework for Solana programs using LiteSVM. Provides ergonomic, type-safe assertions for transaction results, logs, and all levels of Solana errors, plus systematic compute unit (CU) analysis for performance optimization.
β οΈ Development Status: This library is currently in active development. The API may change before the first stable release.
β¨ Features
π§ͺ Testing Framework
- π― Complete Error Testing: Transaction, instruction, and system program errors with type safety
- π Log Assertions: Detailed log content verification with helpful error messages
- π§ Build System Integration: Automatic program compilation for Anchor and Pinocchio
- β‘ Dual API Styles: Direct function calls or fluent method syntax
- πͺ Precision Control: "Anywhere" matching vs surgical instruction-index targeting
- π‘οΈ Type Safety: Work with
SystemError
enums instead of raw error codes - π Educational Examples: Learn API progression from verbose to elegant
π CU Benchmarking Framework
- β‘ Systematic CU Analysis: Measure compute unit usage with statistical accuracy
- π¬ Dual Paradigms: Pure instruction benchmarking vs complete transaction workflows
- π Percentile-Based Estimates: Min, conservative, balanced, safe, very high, and max CU usage
- π― Production-Ready: Generate CU estimates for real-world fee planning
- π Rich Context: Execution logs, program details, and SVM environment state
- π Reproducible: Consistent results across environments and runs
π Framework Support
- Testing: Anchor, Pinocchio, with more coming
- Benchmarking: Universal framework for any Solana program
π Quick Start
Current (Development)
Add to your Cargo.toml
:
[]
= { = "https://github.com/levicook/litesvm-testing" }
= "0.6.1"
Future (Stable Release)
Once published to crates.io, you'll be able to use:
[]
= "0.1.0" # When available
= "0.6.1"
Basic Usage
use *;
Fluent API (Method Style)
use *;
π― Error Testing Hierarchy
Test errors at every level of the Solana execution model:
ποΈ Transaction Level
Validation errors before execution:
result.demand_transaction_error;
π Instruction Level
Errors during instruction execution:
// "Anywhere" - don't care which instruction failed
result.demand_instruction_error;
// "Surgical" - specific instruction must fail
result.demand_instruction_error_at_index;
βοΈ System Program Level
Type-safe system program errors:
// "Anywhere" - system error occurred somewhere
result.demand_system_error;
// "Surgical" - specific instruction produced system error
result.demand_system_error_at_index;
π Framework Support
Anchor Programs
Complete build system integration with IDL support:
[]
= { = "...", = ["anchor"] }
// build.rs
use build_anchor_program;
Pinocchio Programs
Lightweight compilation with minimal boilerplate:
[]
= { = "...", = ["pinocchio"] }
// build.rs
use build_pinocchio_program;
π― Complete Testing Utilities
Log Assertions
// Search all logs for content
result.demand_logs_contain;
// Check specific log entry by index
result.demand_logs_contain_at_index;
Transaction Error Testing
// Test transaction-level failures
result.demand_transaction_error;
result.demand_transaction_error;
Instruction Error Testing
// Test any instruction failure
result.demand_instruction_error;
result.demand_instruction_error;
// Test specific instruction failure
result.demand_instruction_error_at_index;
System Error Testing (Type-Safe)
// Test system program errors anywhere
result.demand_system_error;
result.demand_system_error;
// Test system errors at specific instruction
result.demand_system_error_at_index;
Convenience Setup
// Quick SVM setup with funded fee payer
let = setup_svm_and_fee_payer;
πͺ API Styles: Choose Your Preference
Direct Functions (traditional):
demand_logs_contain;
demand_system_error;
Fluent Methods (alternative syntax):
result.demand_logs_contain;
result.demand_system_error;
Both styles provide identical functionality - choose what feels right for your team!
Note: Chainable fluent methods (
DemandChaining
) are planned for a future release, which will enableresult.demand_x().demand_y().accept()
style chaining.
π Complete Examples
This repository includes comprehensive, documented examples:
Testing Framework
Anchor Framework
- Program:
examples/anchor/simple-anchor-program/
- Tests:
examples/anchor/simple-anchor-tests/
- Features: IDL integration, automatic compilation, complete build documentation
Pinocchio Framework
- Program:
examples/pinocchio/simple-pinocchio-program/
- Tests:
examples/pinocchio/simple-pinocchio-tests/
- Features: Minimal boilerplate, lightweight setup, direct BPF compilation
Educational Test Suite
- API Progression:
tests/test_system_error_insufficient_funds.rs
- Features: Good β Better β Best β Best+ progression, demonstrates all API styles
CU Benchmarking Framework
Instruction Benchmarks
- SOL Transfer:
benches/cu_bench_sol_transfer_ix.rs
- Pure system program instruction (150 CU) - SPL Token Transfer:
benches/cu_bench_spl_transfer_ix.rs
- Complex multi-account instruction
Transaction Benchmarks
- Token Setup Workflow:
benches/cu_bench_token_setup_tx.rs
- Complete 5-instruction workflow (28K-38K CU)
Documentation
- Complete Guide:
BENCHMARKING.md
- Comprehensive benchmarking documentation
πββοΈ Running Examples
# Clone the repository
# Run all tests with detailed output
# Run specific framework tests
# Run educational test suite
# Run CU benchmarks with progress logging
RUST_LOG=info
RUST_LOG=info
π οΈ Prerequisites
- Rust (latest stable)
- Solana CLI tools for
cargo build-sbf
command
Quick Installation (Recommended)
Install all Solana development dependencies with one command:
|
This installs Rust, Solana CLI, Anchor CLI, Node.js, and Yarn all at once.
Manual Installation
If the quick install doesn't work, install the Solana CLI individually:
Note: The Solana CLI is now maintained by Anza (formerly Solana Labs) and uses the Agave validator client.
Verify Installation
Check that everything is installed correctly:
# Check Solana CLI
# Expected: solana-cli 2.2.12 (src:0315eb6a; feat:1522022101, client:Agave)
# Check Rust
# Expected: rustc 1.86.0 (05f9846f8 2025-03-31)
ποΈ Project Structure
litesvm-testing/
βββ crates/
β βββ litesvm-testing/ # Core library with comprehensive docs
β βββ src/
β β βββ lib.rs # Main API and documentation
β β βββ anchor_testing/ # Anchor build utilities
β β βββ pinocchio_testing/ # Pinocchio build utilities
β βββ tests/ # Educational test examples
βββ examples/
β βββ anchor/ # Complete Anchor integration
β β βββ simple-anchor-program/
β β βββ simple-anchor-tests/
β βββ pinocchio/ # Complete Pinocchio integration
β βββ simple-pinocchio-program/
β βββ simple-pinocchio-tests/
βββ README.md # This file
πΊοΈ Roadmap
β Completed Features
- Core log assertion utilities
- Complete error testing framework (transaction, instruction, system)
- Type-safe system error handling
- Anchor and Pinocchio build support with comprehensive documentation
- Working examples for both frameworks with educational progression
- Dual API styles (direct functions + fluent method syntax)
- Precision control ("anywhere" vs "surgical" assertions)
- CU benchmarking framework with instruction and transaction paradigms
- Statistical CU analysis with percentile-based estimates
- Rich benchmarking context (execution logs, program details, SVM state)
π In Progress
- Steel framework support
- Additional testing utilities (account state verification, etc.)
- First stable release (v0.1.0) to crates.io
- Integration with popular Solana testing patterns
π Educational Value
This library is designed not just as a tool, but as a learning resource:
- Progressive examples: See how testing approaches evolve from verbose to elegant
- Framework comparisons: Understand trade-offs between Anchor and Pinocchio
- Complete documentation: Every function includes usage examples and context
- Real error scenarios: Test actual system program failures, not synthetic examples
π License
This project is dual licensed under GPL-3.0-or-later and CC BY-SA 4.0. See LICENSE for details.