Skip to main content
Version: 2.x

mops test

Mops can run Motoko unit tests

mops test

Put your tests in *.test.mo files inside a test/ or tests/ directory (nested subdirectories work too).

If a test/lib.mo (or tests/lib.mo) file exists, it is the only file run — use it as an entry point that imports your other tests.

Pass a filter to run a subset of files — mops test nat runs every *nat*.mo file under the test directory (the .test.mo suffix is not required for filtered files):

mops test nat

All tests run as quickly as possible thanks to parallel execution.

See test package to help you write tests.

Options

--reporter, -r

Test reporter.

--reporter <reporter>

Available reporters:

  • verbose - print each file/suite/test name and Debug.print output
  • files - print only test files
  • compact - pretty progress bar
  • silent - print only errors

Default verbose if there is only one file to test and files otherwise.

note

Only verbose reporter prints Debug.print output.

--watch, -w

Re-run tests every time you change *.mo files.

--watch

--mode

Test run mode

--mode <mode>

Available modes:

  • interpreter - run tests via moc -r (default)
  • wasi - compile test file to wasm and execute it with wasmtime. Useful, when you use to_candid/from_candid, or if you get stackoverflow errors.
  • replica - deploy test files as canisters to a local replica (--replica selects which one). See Replica tests.

You can also specify wasi or replica mode for a specific test file by adding one of these markers to the file (on a line of its own, with nothing else on the line):

// @testmode wasi
// @testmode replica

Test files that define an actor run in replica mode automatically.

--replica

Which replica to use to run actor tests.

Default pocket-ic if pocket-ic is specified in mops.toml in [toolchain] section or MOPS_POCKET_IC_URL points at an already-running PocketIC server, otherwise dfx (deprecated, see below).

Possible values:

  • pocket-ic - use PocketIC light replica via pic.js. Recommended.
  • dfx - deprecated. Uses dfx local replica. Will be removed in a future release. Run mops toolchain use pocket-ic 15.0.0 to pin a PocketIC version and mops test will use it directly.
info

If you run mops test --replica pocket-ic AND neither pocket-ic in [toolchain] nor MOPS_POCKET_IC_URL is set, Mops will use the pocket-ic replica that comes with dfx (dfx start --pocketic). This fallback path is also deprecated.

--verbose

Show replica logs

-- <moc flags>

Pass extra flags directly to the Motoko compiler for this invocation. Appended after [moc].args from mops.toml.

mops test -- -Werror

Replica tests

Replica tests are useful if you need to test actor code which relies on the IC API(cycles, timers, canister upgrades, etc.).

To run replica tests, your test file should look like this:

...

actor {
public func runTests() : async () {
// your tests here
};
};

Example:

import {test} "mo:test/async";
import MyCanister "../my-canister";

actor {
// add cycles to deploy your canister
ExperimentalCycles.add<system>(1_000_000_000_000);

// deploy your canister
let myCanister = await MyCanister.MyCanister();

public func runTests() : async () {
await test("test name", func() : async () {
let res = await myCanister.myFunc();
assert res == 123;
});
};
};

Make sure your actor has runTests method.

See example here.

Under the hood, Mops will:

  • Start a local replica on port 4945
  • Compile test files and deploy them
  • Call runTests method of the deployed canister