Skip to content
Mollusk is a lightweight test harness for testing Solana programs. It provides a simple interface for testing Solana program instructions in a minified Solana Virtual Machine (SVM) environment. All test accounts must be explicitly defined, ensuring deterministic and repeatable tests.

​Installation

Add mollusk-svm as a dependency in Cargo.toml:
Terminal
Cargo.toml
The examples on this page also use solana-sdk, solana-sdk-ids, and the System Program interface. Pin the versions that match Mollusk 0.7:
Terminal
Cargo.toml
Keep the @0.7 pin on every mollusk-svm crate. cargo add mollusk-svm without a version installs a newer Mollusk release that doesn’t compile with these Solana crate versions.
To benchmark compute unit usage, add mollusk-svm-bencher as dependency in Cargo.toml:
Terminal
Cargo.toml
To use the token program, token2022 program (token extensions), and associated token program for testing with Mollusk, add mollusk-svm-programs-token as dependency in Cargo.toml:
Terminal
Cargo.toml

​Mollusk SVM

The following example shows a minimal setup for testing a basic Solana program using Mollusk.
1

Hello World Program

This example demonstrates how to test a basic Solana program using Mollusk. The program simply prints “Hello, world!” to the program logs when invoked.Running cargo build-sbf --arch v0 generates the compiled program at /target/deploy/<program_name>.so. Build with --arch v0: Mollusk 0.7 cannot load programs built with the current default SBPF v3 architecture and cargo test fails with FailedToParse("invalid file header").
src/lib.rs
To test a Solana program with Mollusk:
  1. Create a Mollusk instance - Initialize Mollusk with a program ID and the path to the compiled program (.so file)
  2. Build an instruction - Create an instruction to invoke the program
  3. Process and validate - Process the instruction using Mollusk and validate the result
src/lib.rs
To execute the test, run cargo build-sbf --arch v0 and then cargo test.When the test runs successfully, you’ll see output similar to the following:
Terminal
Cargo.toml
target/deploy/hello_world.so
The Mollusk struct provides a simple interface for testing Solana programs. All fields can be manipulated through a handful of helper methods, but users can also directly access and modify them if they desire more control. To initialize Mollusk with a default instance use the Mollusk::default method.
Example
To initialize Mollusk with a specific program use the Mollusk::new method.
Example
To add a program to Mollusk use the Mollusk::add_program method.
Example
When providing the file path, do not include the .so extension. For example, "path/to/my_program" is correct, but "path/to/my_program.so" is not.

​Processing Instructions

Mollusk provides four main methods for processing instructions:
MethodDescription
process_instructionProcess an instruction and return the result.
process_and_validate_instructionProcess an instruction and perform a series of checks on the result, panicking if any checks fail.
process_instruction_chainProcess multiple instructions and return the result.
process_and_validate_instruction_chainProcess multiple instructions and perform a series of checks on each result, panicking if any checks fail.
The InstructionResult contains the details of a processed instruction.

​Single Instruction

Use the process_instruction method to process a single instruction without checks on the result. You can manually validate the results after processing.
Method Signature
The following example processes a SOL transfer instruction without validation checks.
The examples below run Mollusk in the main function for demonstration purposes. Because mollusk-svm is a dev-dependency, save each example as examples/<name>.rs and run it with cargo run --example <name>. The main function in src/main.rs cannot import dev-dependencies. In practice, you’ll typically use Mollusk in a test module annotated with the #[test] attribute.
Single Instruction

​Single Instruction with Checks

Use the process_and_validate_instruction method to process a single instruction with validation checks. This method will panic if any check fails.
Method Signature
The following example processes a SOL transfer instruction with validation checks.
With Validation

​Multiple Instructions

Use the process_instruction_chain method to process multiple instructions sequentially without validation checks.
Method Signature
The following example processes two SOL transfer instructions without validation checks.
Multiple Instructions

​Multiple Instructions with Checks

Use the process_and_validate_instruction_chain method to process multiple instructions with validation checks after each instruction. Each instruction has its own set of checks that must pass.
Method Signature
The following example processes a chain of two SOL transfer instructions with validation checks after each instruction.
With Validation

​Validation Checks

Mollusk provides a set of helper methods to check the results of a processed instruction.
Example
Use the following methods to validate instruction results:
Example
Use the following to validate Account states:
Example

​Persistent Account State

The MolluskContext is a wrapper around Mollusk that maintains account state across multiple instruction calls through its account_store. The methods for processing instructions are identical to Mollusk. Unlike Mollusk, which requires passing accounts to each method (e.g. process_instruction), MolluskContext manages accounts internally through its account_store. This eliminates the need for the accounts parameter when processing instructions. Create an account_store using the with_context method:
Example
The following example processes two separate SOL transfer instructions with persistent account state between the instructions through the account_store.
Stateful Testing

​Mollusk Sysvars

Mollusk provides a custom Sysvars struct for modifying its values for testing. Use the warp_to_slot method to update the sysvar clock to simulate moving forward or backward in time to a specific slot.
Warp to Slot
The following example shows how to modify the Mollusk sysvar directly by accessing the sysvars field to change the rent parameters. You can modify other sysvar values the same way.
Modify Sysvars

​Compute Unit Benchmarking

MolluskComputeUnitBencher tracks compute unit usage of a program’s instructions. Results are written to a markdown file. Requires mollusk-svm-bencher as a dependency. The following example benchmarks the compute unit usage of a SOL transfer instruction.
Benchmark Compute Units
Save the benchmark as benches/compute_units.rs and register it in Cargo.toml with the default test harness disabled:
Cargo.toml
Run it with cargo bench. You can also save the same file under examples/ and run it with cargo run --example compute_units. The results are written to the specified out_dir as a markdown file named compute_units.md.
compute_units.md

​Token Program Testing

Use the mollusk-svm-programs-token crate add the token program, token2022 program (token extensions), and associated token program to Mollusk for testing.
Example
The following example demonstrates testing a token transfer using Mollusk.
The example below manually defines the test accounts for demonstration purposes. The mollusk-svm-programs-token also includes helper functions to create the mint and token accounts.
Token Transfer Example