Unit testing Rust canisters

I’m currently writing tests for Sudograph, using Rust’s built-in testing capabilities (cargo test and such) and proptest. I spin up a local replica and execute my tests as query or update calls into canisters. That works very well for integration tests, as my tests go through the entire motions of creating query and update calls and validating the responses.

But it’s very slow, even if I use --no-artificial-delay, compared to the speed my tests could move at if they were running from within the canister. Instead of performing http requests to call the canister methods, it would be nice if I could run tests from within the canister that could call the methods directly.

How should we go about testing canister code without the overhead of network requests to perform query and update calls?

I personally find it very useful to keep API methods as simple as possible and have a separate place for the main logic, which I can cover with standard rust unit tests.

For example, consider the following canister

static mut STATE: Option<InfoStorage> = None;

fn init() {
  unsafe {
    STATE = Some(InfoStorage::new())

fn set_info(info: String) {
  let state = unsafe { STATE.as_mut().unwrap() };

fn get_info() -> String {
  unsafe { STATE.as_ref().unwrap().get_info() }

I would suggest to have a separate struct InfoStorage in another file which you could cover with tests:

struct InfoStorage {
  pub info: String;

impl InfoStorage {
  pub fn new() { ... }

  pub fn set_info(info: String) { ... }

  pub fn get_info() -> String { ... }

mod tests {
  fn check_if_everything_works_ok() {
    let storage = InfoStorage::new();

Hi Senior.joinu
How are you?
I am writing a unit test script in rust using ic_cdk::call.
But I got an error

running 1 test
thread 'users::update::it_works' panicked at 'call_new should only be called inside canisters.', /home/eiten/.cargo/registry/src/github.com-1ecc6299db9ec823/ic0-0.18.9/src/ic0.rs:155:9
stack backtrace:

This is source code that I am using.

let res: Result<(i32,), _> = call(
        (String::from("test user"),),

How can I solve this?

1 Like

I’m creating LightIC exactly for that. So you can run integration tests in a fast and efficient manner.

1 Like