Documentazione SolanaLiteSVMRustTest del programma

Avvio Rapido

Configurazione

Prima di testare il tuo programma, assicurati di:

  1. Compilare il tuo programma per generare il file .so
  2. Aggiungere litesvm come dipendenza di sviluppo al file Cargo.toml
  3. Aggiungere eventuali dipendenze di sviluppo aggiuntive che potrebbero essere necessarie, come solana-sdk e litesvm-token
  4. Creare un file di test nella directory tests

Struttura Base del Test del Programma

Ecco tutti i passaggi fondamentali necessari per richiamare ed eseguire un'istruzione in un programma Solana.

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 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!");
}

L'ambiente di test predefinito generato con LiteSVM::new() include tutte le funzionalità runtime abilitate, sysvars predefinite, precompilazioni, programmi spl, verifica delle firme e tutti i programmi integrati, come il System Program.

Se desideri interagire con qualsiasi programma non incluso nell'ambiente di test predefinito, devi distribuire quel programma nell'ambiente di test.

Prossimi Passi

Nella prossima sezione, impareremo tutto su come distribuire i programmi nell'ambiente di test litesvm.

Is this page helpful?

Indice dei contenuti

Modifica pagina
© 2026 Solana Foundation. Tutti i diritti riservati.