10 releases (5 major breaking)

new 6.0.0 Nov 14, 2024
5.0.0 Sep 13, 2024
4.0.0 Jul 23, 2024
3.1.0 May 2, 2024
1.0.0 Oct 12, 2023

#609 in Magic Beans

Download history 2625/week @ 2024-07-30 3372/week @ 2024-08-06 2853/week @ 2024-08-13 3201/week @ 2024-08-20 3138/week @ 2024-08-27 3283/week @ 2024-09-03 3477/week @ 2024-09-10 2273/week @ 2024-09-17 2916/week @ 2024-09-24 3745/week @ 2024-10-01 3193/week @ 2024-10-08 3136/week @ 2024-10-15 3393/week @ 2024-10-22 2653/week @ 2024-10-29 2532/week @ 2024-11-05 2729/week @ 2024-11-12

12,214 downloads per month
Used in 6 crates

Apache-2.0

175KB
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::encode_one;
use pocket_ic::PocketIc;

 #[test]
 fn test_counter_canister() {
    let pic = PocketIc::new();
    // Create an empty canister as the anonymous principal and add cycles.
    let canister_id = pic.create_canister();
    pic.add_cycles(canister_id, 2_000_000_000_000);
    
    let wasm_bytes = load_counter_wasm(...);
    pic.install_canister(canister_id, wasm_bytes, vec![], None);
    // 'inc' is a counter canister method.
    call_counter_canister(&pic, canister_id, "inc");
    // Check if it had the desired effect.
    let reply = call_counter_canister(&pic, canister_id, "read");
    assert_eq!(reply, WasmResult::Reply(vec![0, 0, 0, 1]));
 }

fn call_counter_canister(pic: &PocketIc, canister_id: CanisterId, method: &str) -> WasmResult {
    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.
  • Leave the binary in your current working directory, or specify the path to the binary by setting the POCKET_IC_BIN environment variable before running your tests.
  • 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

~14–28MB
~434K SLoC