Testing
Write tests with meta test, run them with actus test, read passing and failing results, and run only some of them.
A test in Actus is a verb that has the words meta test in front of it.
actus test finds these verbs, runs each one as a program of its own, and
tells you which ones passed. This page uses the shapes project from
Projects and modules.
The project
This page works with the shapes project. Here are its files, from
Projects and modules. The facade of the
geometry module is src/geometry/geometry.act:
The helper is src/geometry/helpers.act:
The verbs of the module are in src/geometry/rect.act:
And src/main.act uses them:
Where tests go
There are two places for tests, and each one has a name:
- Module tests go in a folder called
testsinside the module they test, next to its facade. Tests of the modulegeometrygo insrc/geometry/tests/. They are part of the module, so they can use its private verbs. - Project tests go in the folder
testsnext tosrc. They test the program from outside, the way another module would use it. They can use only what the modules makeopen.
Put the .act files directly in the folder. A tests folder cannot have
folders of its own. Production code never sees a tests folder: actus build,
actus run and actus check skip it.
Write a module test
The geometry module has the verbs area and perimeter, which are open.
Add one private verb to src/geometry/rect.act, a verb without open that
only the module can call. This is the whole file now:
Now create src/geometry/tests/rect.act:
A test is a verb with no parameters that returns an Int. It passes when it
returns 0. Any other number means it failed.
The file is in the geometry module, so the tests call area, perimeter
and even the private is_square without an import. The project now looks
like this:
Run the tests from the project folder:
Each line shows the name of the test (the module, then the verb), the result, the time the test took to run and the exit code of the program. The time is only the run of the test. Building is counted in the last line, which gives the totals. The times on your computer will be different.
The output of a test is not shown while it passes. When a test fails, you see what it wrote to the standard error, and nothing else.
One build for each folder
actus test builds all the tests of one tests folder as one program, once,
and then starts that program one time for each test. Every test still runs in a
process of its own. A test that fails or stops does not stop the others. A
project with two hundred tests in one folder is built once, not two hundred
times.
The lines always come in the order of the test names, whichever test finishes first.
Write a project test
A project test goes in the tests folder next to src. It is outside the
module, so it must import what it uses. Create tests/smoke.act:
A module test is named <module>::<verb>. A project test is named
tests::<verb>. The name does not contain the name of the file.
A project test sees only the open verbs that the facade of a module exports.
It cannot call a private verb. Add a second file, tests/private.act, that
tries to call is_square:
This is the same error as in Projects and modules.
No test runs, because the tests do not build. Remove the file. A test of
is_square belongs in a module test, as above. If is_square should be part of
what other code can use, make it open in the module.
Share a helper between the files of a folder
All the files in one tests folder share one set of names, as if they were
one file. A verb that you write in one file can be used in the others without
an import. Create tests/helpers.act:
Now tests/smoke.act can use it. Change the comparison to
!= expected_area(). The tests pass as before.
Because the names are shared, two files in the same folder cannot declare the
same name. A second tests/other.act with its own expected_area fails, and
the error names both files:
The module tests of src/geometry/tests/ share their names with each other in
the same way, and with the module itself.
A failing test
Remove tests/private.act. Then change the expected perimeter in the second
module test from 26 to 27 and run again:
The failed test returned 1, which is the exit code on its line. actus test
exits with status 1 when any test fails, so a script or a CI job can tell:
Test results do not say why a test returned 1. If you want to tell the checks of one test apart, return a different number for each, such as 1 for the first check and 2 for the second. The number shows up as the exit code.
Change the number back to 26.
A test that hits a runtime check
Some mistakes stop a program while it runs, as in
First program. The same
happens in a test. Add this test to the end of src/geometry/tests/rect.act.
It multiplies two u8 numbers whose product does not fit in a u8:
The exit code is 101, which is the code Actus uses when a runtime check
fails. Because the test crashed, actus test also shows what the program
wrote to the standard error: the check that failed, and the file, line and
column. Here that is the multiplication at line 34, column 21. Division by
zero is reported in the same way, as error: division by zero at ....
To run only this test, give actus test a part of its name:
The first line says running 1 test, with the singular word for one test. The
(4 filtered) counts every other test of the project that was left out, in
all folders: the three other tests of the geometry module and the project
test tests::area_through_the_facade.
When a test is not valid
A test must have no parameters and must return an Int. When it does not,
actus test reports that instead of running it. This test in tests/bad.act
has a parameter:
Only that test fails. Some mistakes are found when the program is built, such as a test that returns nothing:
Because the tests of a folder are built as one program, a mistake that stops
the build fails every test of that folder. The error is shown once, with the
first test, and the other tests point to it. Tests in other folders are not
affected. Remove tests/bad.act before you go on.
If a test never ends, actus test stops it after 30 seconds. Set
ACTUS_TEST_TIMEOUT_SECONDS to use another limit. Here it is 2 seconds, for a
test with an endless loop:
Tests in the wrong place
Tests have two places, and actus test tells you when a test is somewhere else.
A meta test verb in a normal source file, for example in
src/geometry/rect.act, is an error that names the folder it should move to:
src/main.act is the root of the project and has no tests folder. A test
there gets a different help text:
A file in a folder inside tests, such as tests/geometry/rect.act, is an
error too. The message says where the file belongs:
The same code, E1143, is used when a module tests folder has a folder
inside it, when there is a src/tests folder, and when a tests folder has a
facade tests.act. A tests folder is never a module.
Files that a test writes
Each test runs in a folder of its own. The folder is new and empty when the
test starts, and actus test removes it afterwards. So a test that creates a
file should use a relative path, such as result.txt. The file is then created
in that folder. Two tests can use the same name without disturbing each other,
and nothing is left in your project. A path that starts at your project folder
or at a fixed place on your disk does not belong in a test: the test would
write outside its own folder.
Choose which tests run
You have already used a word to pick a test. actus test runs only the tests
whose name contains the word. This one picks the two tests with of_six in
their name:
The first line of the output says how many tests will run and how many were
left out. (3 filtered) here means that three other tests of the project do
not have of_six in their name. The count includes the tests of every folder,
not only the folder of the tests that run.
These options select tests, or change how they run:
| Option | Effect |
|---|---|
--list | print the names of the selected tests and do not run them |
--exact | the word must be the whole name of the test, not a part of it |
--file <path> | only tests in files whose path contains <path> |
--module <path> | only tests in modules whose name contains <path> |
--test-threads <n> | run at most n tests at the same time. The default is the number of processor cores |
--release, --profile <name> | build the test programs with that profile, as for actus build. The default is the profile of Actus.toml |
--strict | use the strict rules. See Everyday commands |
Try --list first when you are not sure what a word will select:
Then pick one test by its verb name or by its whole name. --exact accepts
both:
Or all the tests of a file or a module. The module name is the name before the
verb, and tests is the name of the project tests:
The first two print the four tests of the geometry module, and their first
line says running 4 tests (1 filtered), because the project test is left
out. The last prints running 1 test (4 filtered) and then
tests::area_through_the_facade. With --test-threads 1 the tests run one at
a time. The lines are in the same order as always, so only the speed changes:
By default the tests are built with the profile of Actus.toml, which is
debug in a new project. With --release or --profile <name> they are
built with that profile, and the first line of the output names it:
What you learned
- A test is a verb with
meta testin front of it. It has no parameters and returns anInt. Returning 0 means it passed. - Module tests go in
src/<module>/tests/. They are part of the module and can use its private verbs. Project tests go intests/and see only theopenverbs of the modules they import. Neither folder has folders inside. - All files of one
testsfolder share their names, so a helper can be used by all of them and a name cannot be used twice. - Tests are named
<module>::<verb>ortests::<verb>.actus testbuilds each folder once and runs each test in its own process, in an empty folder. actus testprints one line per test, in the order of the names, and exits with status 1 if any test failed.- A runtime check that fails inside a test gives exit code 101 and shows
error: <check> at file:line:col. - A word,
--exact,--fileand--modulechoose tests.--listshows what they select.
Next: Everyday commands.