솔라나 문서LiteSVMRust프로그램 테스트

빠른 시작

설정

프로그램을 테스트하기 전에 다음을 확인하세요:

  1. .so 파일을 생성하기 위해 프로그램을 빌드하세요
  2. litesvmCargo.toml 파일의 dev 의존성으로 추가하세요
  3. solana-sdklitesvm-token와 같이 필요한 추가 dev 의존성을 추가하세요
  4. tests 디렉토리에 테스트 파일을 생성하세요

기본 프로그램 테스트 구조

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

LiteSVM::new()로 생성되는 기본 테스트 환경에는 모든 런타임 기능 활성화, 기본 sysvar, 사전 컴파일, spl 프로그램, sigverify, 그리고 System Program을 포함한 모든 내장 프로그램이 포함되어 있습니다.

기본 테스트 환경에 포함되지 않은 프로그램과 상호작용하려면 해당 프로그램을 테스트 환경에 직접 배포해야 합니다.

다음 단계

다음 섹션에서는 litesvm 테스트 환경에 프로그램을 배포하는 방법에 대해 자세히 알아보겠습니다.

Is this page helpful?

목차

페이지 편집