This repository contains two example Solana programs (caller and called) and two corresponding tests:
- a ProgramTest-based test that runs in-process (
cross_program_invocation_program_test), and - a validator-based test that uses a running local validator and deployed programs (
cross_program_invocation_validator_test).
Important: Tests must now be run separately! There’s no point in running all tests together (e.g.,
cargo test -- --nocapture), because the ProgramTest-based test relies on.sobinaries without an entrypoint, while the validator-based test requires.sofiles with an entrypoint. Therefore, each test must be built and run separately, with its own dedicated build.
First, build both contracts:
cargo build-sbf --manifest-path=code/caller/Cargo.toml
cargo build-sbf --manifest-path=code/called/Cargo.tomlImportant: The
--features disable-entrypointflag must not be used for validator-based tests.
Launch a local Solana test validator:
solana-test-validatorSee
snippets.mdfor an advanced validator launch command that helps avoid WSL-related issues.
Deploy both contracts to the local validator:
solana program deploy target/deploy/called.so
solana program deploy target/deploy/caller.soImportant: After deployment, update the
pubkeyvariables for the smart contracts in the tests with the actual addresses returned by the deploy commands.
Run the validator-based test:
cargo test -p tests cross_program_invocation_validator_test -- --nocaptureFirst, build both contracts:
cargo build-sbf --manifest-path=code/caller/Cargo.toml --features disable-entrypoint
cargo build-sbf --manifest-path=code/called/Cargo.toml --features disable-entrypointImportant: The
--features disable-entrypointflag is required for ProgramTest-based tests.
Run the ProgramTest-based test:
cargo test -p tests cross_program_invocation_program_test -- --nocaptureAll useful command shortcuts are stored in
snippets.md.