Testing Your Program
Quick Start
Learn how to test your Solana program with LiteSVM
Setup
Before testing your program, ensure you:
- Build your program to generate the
.so
file - Add
litesvm
as a dev dependencies to theCargo.toml
file - Add any additional dev dependencies that may be needed, like
solana-sdk
andlitesvm-token
- Create a test file in your
tests
directory
Basic Program Test Layout
Here are all the basic steps needed to call and execute an instruction in a Solana program.
use litesvm::LiteSVM;
use solana_sdk::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
};
use my_program;
#[test]
fn test_my_program() {
// Initialize the test environment
let mut svm = LiteSVM::new();
// Deploy your program to the test environment
let program_id = Pubkey::from(my_program::ID);
let program_bytes = include_bytes!("../../../target/deploy/my_program.so");
svm.add_program(program_id, program_bytes);
// Create and fund test accounts
let payer = Keypair::new();
svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();
// Create your instruction
let instruction = Instruction {
program_id,
accounts: vec![
AccountMeta::new(payer.pubkey(), true),
],
data: vec![],
};
// Build transaction
let tx = Transaction::new_signed_with_payer(
&[instruction],
Some(&payer.pubkey()),
&[&payer],
svm.latest_blockhash(),
);
// Send transaction
let result = svm.send_transaction(tx).unwrap();
// Check transaction succeeded
println!("Transaction logs: {:?}", result.logs);
println!("Program executed successfully!");
}
The default test environment that is generated with LiteSVM::new()
includes
all runtime features enabled, default sysvars, precompiles, spl programs,
sigverify, and all built in programs, like the system program.
If you want to interact with any program that is not included in the default test environment, you must deploy that program to the test environment.
Next Steps
In the next section, we'll learn all about deploying programs to the litesvm test environment.