Motoko unit testing

@kpeacock in response to your concern here:

“I also like that I can run my full set of unit tests and get the results of all of them, even if some are failing”

I was just playing around with this this today, and if you look at suite() function definition in motoko-matchers, you can actually calls to suite within one another.

This means that you make a testing pattern like

let clazz1Suite = suite(...);
let clazz2Suite = suite(...);
let clazz3Suite = suite(...);

run(suite("all tests", [
  clazz1Suite,
  clazz2Suite,
  clazz3Suite
]));

Additionally, if you wanted to have these tests in multiple files, you would do the following.

// TestClazz1.mo
// all imports

module {
  public let clazz1Suite = suite(...)
}

Repeat the above over all your class files, then…

// RunTests.mo
import TestClazz1 "./TestClazz1";
import TestClazz2 "./TestClazz2";
import TestClazz3 "./TestClazz3";
// other imports

run(suite("all tests", [
  TestClazz1.clazz1Suite,
  TestClazz2.clazz2Suite,
  TestClazz3.clazz3Suite
]));

I’d imagine that this could be automated further (removing the need to wrap each file test in a module and the import step) by having some sort of bash script loop

for file in *.mo; do \
  ...run each file, write errors to a file

then have another script (node/python) grab this and pretty print it.

@kritzcreek curious if you had any original intentions of how to build this out instead of the current exit(1) on failures within run(). I’d be interested in making a few contributions, but want to vet the idea before starting work/opening a PR, etc.

3 Likes