11 releases (6 major breaking)

7.0.0 Feb 27, 2025
6.0.0 Nov 14, 2024
5.0.0 Sep 13, 2024
4.0.0 Jul 23, 2024
1.0.0 Oct 12, 2023

#5 in #smart-contracts

Download history 3831/week @ 2024-12-04 4092/week @ 2024-12-11 1953/week @ 2024-12-18 567/week @ 2024-12-25 1644/week @ 2025-01-01 3966/week @ 2025-01-08 3908/week @ 2025-01-15 3509/week @ 2025-01-22 3866/week @ 2025-01-29 4390/week @ 2025-02-05 3543/week @ 2025-02-12 3419/week @ 2025-02-19 4450/week @ 2025-02-26 3955/week @ 2025-03-05 3443/week @ 2025-03-12 3338/week @ 2025-03-19

15,789 downloads per month
Used in 9 crates

Apache-2.0

195KB
4K SLoC

PocketIC Rust: A Canister Testing Library

PocketIC is a local canister testing solution for the Internet Computer.
This testing library works together with the PocketIC server, allowing you to interact with your local IC instances and the canisters thereon.

With PocketIC Rust, testing canisters is as simple as calling Rust functions. Here is a simple example:

use candid::{Principal, encode_one};
use pocket_ic::PocketIc;

// 2T cycles
const INIT_CYCLES: u128 = 2_000_000_000_000;

#[test]
fn test_counter_canister() {
    let pic = PocketIc::new();

    // Create a canister and charge it with 2T cycles.
    let canister_id = pic.create_canister();
    pic.add_cycles(canister_id, INIT_CYCLES);

    // Install the counter canister wasm file on the canister.
    let counter_wasm = todo!();
    pic.install_canister(canister_id, counter_wasm, vec![], None);

    // Make some calls to the canister.
    let reply = call_counter_can(&pic, canister_id, "read");
    assert_eq!(reply, vec![0, 0, 0, 0]);
    let reply = call_counter_can(&pic, canister_id, "write");
    assert_eq!(reply, vec![1, 0, 0, 0]);
    let reply = call_counter_can(&pic, canister_id, "write");
    assert_eq!(reply, vec![2, 0, 0, 0]);
    let reply = call_counter_can(&pic, canister_id, "read");
    assert_eq!(reply, vec![2, 0, 0, 0]);
}

fn call_counter_can(pic: &PocketIc, canister_id: Principal, method: &str) -> Vec<u8> {
    pic.update_call(
        canister_id,
        Principal::anonymous(),
        method,
        encode_one(()).unwrap(),
    )
    .expect("Failed to call counter canister")
}

Getting Started

Quickstart

  • Download the latest PocketIC server from the PocketIC repo that is compatible with the library version you're using.
  • Ungzip the downloaded file.
  • On UNIX: make the downloaded file executable.
  • Specify the path to the binary by using the function PocketIcBuilder::with_server_binary or the environment variable POCKET_IC_BIN.
  • Add PocketIC Rust to your project with cargo add pocket-ic.
  • Import PocketIC with use pocket_ic::PocketIc, and create a new PocketIC instance with let pic = PocketIc::new() in your Rust code and start testing!

Examples

For simple but complete examples, see integration tests.

To see a minimalistic setup of PocketIC in a Rust project, check out the ICP Hello World Rust repository.

For larger test suites with more complex test setups, consider the OpenChat integration test suite. Note that instances are shared among test cases there, which is not recommended in general.

Documentation

Contributing

If you decide to contribute, we encourage you to announce it on the Forum!

Dependencies

~15–30MB
~456K SLoC