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 andDebug.printoutputfiles- print only test filescompact- pretty progress barsilent- print only errors
Default verbose if there is only one file to test and files otherwise.
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 viamoc -r(default)wasi- compile test file to wasm and execute it withwasmtime. Useful, when you useto_candid/from_candid, or if you get stackoverflow errors.replica- deploy test files as canisters to a local replica (--replicaselects 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. Usesdfxlocal replica. Will be removed in a future release. Runmops toolchain use pocket-ic 15.0.0to pin a PocketIC version andmops testwill use it directly.
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
runTestsmethod of the deployed canister