설정
프로그램을 테스트하기 전에 다음을 확인하세요:
.so파일을 생성하기 위해 프로그램을 빌드하세요litesvm를Cargo.toml파일의 dev 의존성으로 추가하세요solana-sdk및litesvm-token와 같이 필요한 추가 dev 의존성을 추가하세요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 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!");}
LiteSVM::new()로 생성되는 기본 테스트 환경에는 모든 런타임 기능 활성화, 기본
sysvar, 사전 컴파일, spl 프로그램, sigverify, 그리고 System Program을 포함한
모든 내장 프로그램이 포함되어 있습니다.
기본 테스트 환경에 포함되지 않은 프로그램과 상호작용하려면 해당 프로그램을 테스트 환경에 직접 배포해야 합니다.
다음 단계
다음 섹션에서는 litesvm 테스트 환경에 프로그램을 배포하는 방법에 대해 자세히 알아보겠습니다.
Is this page helpful?