Setup
Before testing your program, ensure you:
- Build your program to generate the
.sofile - Add
litesvmas a dev dependency to theCargo.tomlfile - Add any additional dev dependencies that may be needed, like
solana-sdkandlitesvm-token - Create a test file in your
testsdirectory
Basic Program Test Layout
Here are all the basic steps needed to call and execute an instruction in a Solana program.
tests/program_test.rs
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 environmentlet mut svm = LiteSVM::new();// Deploy your program to the test environmentlet 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 accountslet payer = Keypair::new();svm.airdrop(&payer.pubkey(), 10_000_000_000).unwrap();// Create your instructionlet instruction = Instruction {program_id,accounts: vec![AccountMeta::new(payer.pubkey(), true),],data: vec![],};// Build transactionlet tx = Transaction::new_signed_with_payer(&[instruction],Some(&payer.pubkey()),&[&payer],svm.latest_blockhash(),);// Send transactionlet result = svm.send_transaction(tx).unwrap();// Check transaction succeededprintln!("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.
Is this page helpful?