ic-test
ic-test is a command-line tool that helps to set up and manage Rust canister tests on the Internet Computer (IC) using. It makes it easier to create a test project and includes the basic files and setup needed for both IC canisters and optionally EVM (Ethereum Virtual Machine) smart contracts.
The tool reads the dfx.json (must exist) and the foundry.toml (may exist) files in order to build the test environment automatically. It uses pocket-ic and alloy (foundry) to run tests.
The generated code and helpers provide:
- A simple way to start a test project.
- A single, easy-to-use interface for testing both IC and EVM parts.
- Type checking and auto-completion support.
- Easy functions for deploying and calling canisters or contracts.
Overview
ic-test will:
- Read
dfx.jsonto get canister details. - Read
foundry.tomlto get contract details. - Generate Rust types from Candid (
.did) files. - Generate contract interfaces from Solidity (
.sol) files. - Provide API to work with
.wasmcanisters and.jsoncontract files in tests.
Requirements
- Rust
- DFX – to build and locally deploy canisters.
- Foundry – optional, if you want to test EVM contract's interaction with canisters.
Installation
Tool usage
Without arguments it starts in interactive mode to create a new test project. If an ic-test.json config file exists already, the "update" mode will regenerate the existing test project bindings.
Create a new test project
- Creates a new test project in the
testsfolder. - Looks for canisters and contracts, generates API bindings and a sample test.
- Generates an
ic-test.jsonconfiguration file. - Fails if the
testsfolder already exists, the user would need to choose a different name.
Update/regenerate an existing test project
Regenerates bindings using the configuration in ic-test.json.
"Hello world" tutorial
Create a "Hello, World!" canister:
Compile the project:
Generate test bindings
If there are uncommitted changes, either commit them before generating or use the --force flag:
This creates a tests package with:
- Canister API bindings in
tests/src/bindings - Test environment setup logic in
test_setup.rs - A test template in
tests.rs
Example test
Edit tests.rs:
use IcpTest;
use cratetest_setup;
async
Run tests:
Adding a counter
Update the canister backend:
use RefCell;
thread_local!
Update Candid file hello-ic-test-backend.did:
service : (nat64, nat64) -> {
"greet": (text) -> (text) query;
"get_counter": () -> (nat64) query;
"increment_counter": () -> ();
}
Set initialization arguments in dfx.json:
Regenerate the bindings:
The ic-test will enter interactive mode and prompt user to allow overwriting the test_setup.rs file. Upon confirmation the the test_setup.rs is regenerated with the initialization parameters:
//...
let hello_ic_test_backend = deploy
.call
.await;
// ...
New test
Add a new test in tests.rs:
async
More examples
For other examples, see [https://github.com/wasm-forge/ic-test-examples].