I’m excited to announce a new feature in Mops that makes it even easier to run unit tests for your Motoko code.
With the latest update, you can run unit tests with the command mops test in your terminal.
To take advantage of this new feature, you’ll need to update your Mops CLI to the latest version.
When running your tests, Mops will now process the new Mops Message Format, which includes special labels on stdout, like so mops:1:start <test_name> . Mops will then parse and format this output to provide clear and concise test results.
Test files:
• test/simple.test.mo
--------------------------------------------------
Running test/simple.test.mo
✓ simple test
✓ test my number
PASS
--------------------------------------------------
Tests passed
Done in 0.17s, passed 2
Testing actor classes
You can even test your actor classes with some limitations:
You cannot call methods from base/ExperimentalCycles
Do I understand it correctly that all dependencies required for tests should be under [dev-dependencies] in the mops.toml on the top-level project directory? And there is no mops.toml file in the test directory, right?
Does the [dev-dependencies] have any other purpose besides for mops test?
If you have published a package with dev dependencies, they will not be installed when someone installs your package.
If you are not going to publish a package, there is no difference between dev and non-dev deps.
Interesting. So when that is set then I don’t even need dfx installed at all. But how is that possible? Without dfx it would need at least wasmtime to run the tests, or? It works for me with only moc and mops. I don’t explicitly install wasmtime nor dfx. How is that possible?
This is great news! Well done to you for creating new testing tools. I tried to write tests using them and it makes work much easier. Thank you for your work! But do you ever plan to develop a coverage checker?
Hi guys, I want to use custom users call my test canister. But I do not know how to set different accounts.
This example show I can create an agent:
import Debug "mo:base/Debug";
actor class MyCanister() {
public query ({caller}) func getCaller() : async Principal {
caller;
};
};
actor class Agent(canister : MyCanister) {
public func call() : async Principal {
await canister.getCaller();
};
};
let myCanister = await MyCanister();
let agent1 = await Agent(myCanister);
let agent2 = await Agent(myCanister);
Debug.print(debug_show(await agent1.call()));
Debug.print(debug_show(await agent2.call()));
But it seems the callers will be a canister not a user. At the same time I have to implement all the target functions in the agent. So any way to set the caller in mops test?
Time.now() seems to report 42. Is there a way to manipulate that? I need to test some things where I set created_at time to a day ago and my parameter is Nat64 so I can’t go negativie.