# Best practices for integration tests of Rust canisters

**URL:** <https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320>\
**Category:** Developers\
**Created:** [September 26, 2023, 2:41pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320 "2023-09-26T14:41:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dustin](https://avatars.discourse-cdn.com/v4/letter/d/5daacb/32.png) [@Dustin](https://forum.dfinity.org/u/Dustin)\
**Post date:** [September 26, 2023, 2:41pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/1 "2023-09-26T14:41:37Z")

</div>

Are there any best practices / guides available for integration testing of Rust canisters or canisters in general?  
I’ve found [this guide](https://kyle-peacock.com/blog/dfinity/simple-canister-e2e/) that was written a while back. Are there any other useful resources available?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [September 26, 2023, 3:11pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/2 "2023-09-26T15:11:30Z")

</div>

If you are ok skipping the frontend I like to use the [state machine](https://github.com/dfinity/test-state-machine-client) for anything where unit tests are not good enough

---

<div class="post-metadata">

**Author:** ![Gwojda](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@Gwojda](https://forum.dfinity.org/u/Gwojda)\
**Post date:** [September 26, 2023, 4:53pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/3 "2023-09-26T16:53:14Z")

</div>

Hello,

As each integration test has to run in a clean environment, how would you re-init each canister state between each test?  
Using -Mode reinstall from dfx currently seems to be the only solution?  
Do you have any other solutions/ideas?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [September 26, 2023, 5:09pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/4 "2023-09-26T17:09:09Z")

</div>

If you use the StateMachine it’s a _lot_ faster. But yes, reinstalling is the only way to get a clean state on a replica

---

<div class="post-metadata">

**Author:** ![Gwojda](https://avatars.discourse-cdn.com/v4/letter/g/5fc32e/32.png) [@Gwojda](https://forum.dfinity.org/u/Gwojda)\
**Post date:** [September 27, 2023, 7:00am UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/5 "2023-09-27T07:00:02Z")

</div>

I’ve never used it, do you have any information or documentation on it?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [September 27, 2023, 7:28am UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/6 "2023-09-27T07:28:14Z")

</div>

I’m waiting for one of our repos to be open-sourced so I have a nice example to show. Probably only a few days away. Setting up the binary is described [here](https://github.com/dfinity/test-state-machine-client). And here’s a few snippets to get you started:

Creating a StateMachine:

```rust
fn new_state_machine() -> StateMachine {
    let mut state_machine_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .unwrap()
        .to_path_buf();

    state_machine_path.push("ic-test-state-machine");

    if !state_machine_path.exists() {

        panic!("state machine binary does not exist");
    }
    StateMachine::new(state_machine_path.to_str().unwrap(), false)
}

```

Then you can write utility functions like this:

```rust
pub fn transfer(
    env: &StateMachine,
    ledger_id: Principal,
    from: Account,
    args: TransferArgs,
) -> Result<Nat, TransferError> {
    let arg = Encode!(&args).unwrap();
    if let WasmResult::Reply(res) = env
        .update_call(ledger_id, from.owner, "icrc1_transfer", arg)
        .unwrap()
    {
        Decode!(&res, Result<candid::Nat, TransferError>).unwrap()
    } else {
        panic!("transfer rejected")
    }
}

```

And with a bunch of those your tests can look like this:

```rust
#[test]
fn test_deposit_flow() {
    let env = &new_state_machine();
    let ledger_id = install_ledger(env);
    let depositor_id = install_depositor(env, ledger_id);
    let user = Account {
        owner: Principal::from_slice(&[0]),
        subaccount: None,
    };

    // Check that the total supply is 0
    assert_eq!(total_supply(env, ledger_id), 0u128);

    // Check that the user doesn't have any tokens before the first deposit.
    assert_eq!(balance_of(env, ledger_id, user), 0u128);

    // Make the first deposit to the user and check the result.
    let deposit_res = deposit(env, depositor_id, user, 1_000_000_000);
    assert_eq!(deposit_res.txid, Nat::from(0));
    assert_eq!(deposit_res.balance, Nat::from(1_000_000_000));
...

```

---

<div class="post-metadata">

**Author:** ![stopak](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/stopak/32/9896_2.png) [@stopak](https://forum.dfinity.org/u/stopak)\
**Post date:** [September 27, 2023, 12:30pm UTC](https://forum.dfinity.org/t/best-practices-for-integration-tests-of-rust-canisters/23320/7 "2023-09-27T12:30:00Z")

</div>

You can also try to use `lightic`, nodejs package. It simulates replica in nodejs context, so you can deploy and test multi canister projects (works with any canister compiled to wasm, so both rust and motko).
