Testing

View source

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:

actus
open rect;

open unit helpers;

The helper is src/geometry/helpers.act:

actus
open verb doubled(abs value: Int) -> Int {
    return value * 2;
}

The verbs of the module are in src/geometry/rect.act:

actus
open verb area(abs width: Int, abs height: Int) -> Int {
    return width * height;
}

open verb perimeter(abs width: Int, abs height: Int) -> Int {
    return doubled(value: abs (width + height));
}

And src/main.act uses them:

actus
import geometry;

verb main() {
    erg width = 6;
    erg height = 7;
    println(int: geometry::area(width: abs width, height: abs height));
    println(int: geometry::perimeter(width: abs width, height: abs height));
}
bash
actus run
text
42
26
process exited with status 0

Where tests go

There are two places for tests, and each one has a name:

  • Module tests go in a folder called tests inside the module they test, next to its facade. Tests of the module geometry go in src/geometry/tests/. They are part of the module, so they can use its private verbs.
  • Project tests go in the folder tests next to src. They test the program from outside, the way another module would use it. They can use only what the modules make open.

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:

actus
open verb area(abs width: Int, abs height: Int) -> Int {
    return width * height;
}

open verb perimeter(abs width: Int, abs height: Int) -> Int {
    return doubled(value: abs (width + height));
}

verb is_square(abs width: Int, abs height: Int) -> Bool {
    return width == height;
}

Now create src/geometry/tests/rect.act:

actus
meta test
verb area_of_six_by_seven() -> Int {
    erg width = 6;
    erg height = 7;
    if area(width: abs width, height: abs height) != 42 {
        return 1;
    }
    return 0;
}

meta test
verb perimeter_of_six_by_seven() -> Int {
    erg width = 6;
    erg height = 7;
    if perimeter(width: abs width, height: abs height) != 26 {
        return 1;
    }
    return 0;
}

meta test
verb five_by_five_is_a_square() -> Int {
    erg side = 5;
    if is_square(width: abs side, height: abs side) {
        return 0;
    }
    return 1;
}

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:

text
shapes/
├── Actus.toml
├── src/
│   ├── main.act
│   └── geometry/
│       ├── geometry.act
│       ├── helpers.act
│       ├── rect.act
│       └── tests/
│           └── rect.act

Run the tests from the project folder:

bash
actus test
text
running 3 tests (0 filtered)
test geometry::area_of_six_by_seven ... ok (11 ms, exit code 0)
test geometry::five_by_five_is_a_square ... ok (11 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (11 ms, exit code 0)
test result: ok. 3 passed; 0 failed; finished in 158 ms

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:

actus
import geometry;

meta test
verb area_through_the_facade() -> Int {
    erg width = 6;
    erg height = 7;
    if geometry::area(width: abs width, height: abs height) != 42 {
        return 1;
    }
    return 0;
}
bash
actus test
text
running 4 tests (0 filtered)
test geometry::area_of_six_by_seven ... ok (10 ms, exit code 0)
test geometry::five_by_five_is_a_square ... ok (10 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (11 ms, exit code 0)
test tests::area_through_the_facade ... ok (11 ms, exit code 0)
test result: ok. 4 passed; 0 failed; finished in 155 ms

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:

actus
import geometry;

meta test
verb square_check() -> Int {
    erg side = 5;
    if geometry::is_square(width: abs side, height: abs side) {
        return 0;
    }
    return 1;
}
bash
actus test
text
error: ./tests/private.act: error[E1109] at 6:8: private declaration `is_square` from module `geometry` is not exported by facade `./src/geometry/geometry.act`; help: mark `is_square` as `open` in its module to export it

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:

actus
verb expected_area() -> Int {
    return 42;
}

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:

text
error: ./tests/other.act: error[E1106] at 1:1: duplicate verb declaration `expected_area` in `./tests/helpers.act` and `./tests/other.act`

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:

bash
actus test
text
running 4 tests (0 filtered)
test geometry::area_of_six_by_seven ... ok (11 ms, exit code 0)
test geometry::five_by_five_is_a_square ... ok (11 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... FAILED (11 ms, exit code 1)
test tests::area_through_the_facade ... ok (13 ms, exit code 0)
test result: FAILED. 3 passed; 1 failed; finished in 201 ms

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:

echo $?
text
1

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:

actus
meta test
verb area_does_not_overflow() -> Int {
    erg wide: u8 = 200u8;
    erg tall: u8 = 2u8;
    erg total: u8 = wide * tall;
    return 0;
}
bash
actus test
text
running 5 tests (0 filtered)
test geometry::area_does_not_overflow ... FAILED (10 ms, exit code 101); stderr=```
error: integer overflow at src/geometry/tests/rect.act:34:21
```
test geometry::area_of_six_by_seven ... ok (10 ms, exit code 0)
test geometry::five_by_five_is_a_square ... ok (10 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (11 ms, exit code 0)
test tests::area_through_the_facade ... ok (12 ms, exit code 0)
test result: FAILED. 4 passed; 1 failed; finished in 158 ms

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:

bash
actus test overflow
text
running 1 test (4 filtered)
test geometry::area_does_not_overflow ... FAILED (12 ms, exit code 101); stderr=```
error: integer overflow at src/geometry/tests/rect.act:34:21
```
test result: FAILED. 0 passed; 1 failed; finished in 137 ms

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:

actus
meta test
verb has_arg(abs value: Int) -> Int {
    return 0;
}
bash
actus test --module tests
text
running 2 tests (4 filtered)
test tests::has_arg ... FAILED (0 ms, meta test verbs must not have parameters)
test tests::area_through_the_facade ... ok (12 ms, exit code 0)
test result: FAILED. 1 passed; 1 failed; finished in 205 ms

Only that test fails. Some mistakes are found when the program is built, such as a test that returns nothing:

actus
meta test
verb returns_nothing() {
}
text
running 2 tests (4 filtered)
test tests::returns_nothing ... FAILED (0 ms, ./tests/bad.act: error[E1141]: `returns_nothing` returns nothing, so its call has no value to bind or use; call it as a statement on its own)
test tests::area_through_the_facade ... FAILED (0 ms, test group `tests` failed to build; the diagnostic is reported with `tests::returns_nothing`)
test result: FAILED. 0 passed; 2 failed; finished in 0 ms

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:

ACTUS_TEST_TIMEOUT_SECONDS=2 actus test never_ends
text
running 1 test (5 filtered)
test tests::never_ends ... FAILED (2003 ms, timed out after 2 seconds)
test result: FAILED. 0 passed; 1 failed; finished in 2.0 s

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:

text
error: ./src/geometry/rect.act: error[E1144] at 14:1: `meta test` verb `in_the_wrong_place` is in production source `./src/geometry/rect.act`; move it to a file in `./src/geometry/tests`

src/main.act is the root of the project and has no tests folder. A test there gets a different help text:

text
error: ./src/main.act: error[E1144] at 11:1: `meta test` verb `in_main` is in production source `./src/main.act`; the root unit has no tests directory: move its logic into a module and test it there, or test it from `tests/` through what it opens

A file in a folder inside tests, such as tests/geometry/rect.act, is an error too. The message says where the file belongs:

text
error: ./tests/geometry/rect.act: error[E1143]: invalid test layout at `./tests/geometry/rect.act`: project tests are directly in `tests/`, not in subdirectories; move it to `./src/geometry/tests/rect.act`

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:

bash
actus test of_six
text
running 2 tests (3 filtered)
test geometry::area_of_six_by_seven ... ok (11 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (12 ms, exit code 0)
test result: ok. 2 passed; 0 failed; finished in 136 ms

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:

OptionEffect
--listprint the names of the selected tests and do not run them
--exactthe 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
--strictuse the strict rules. See Everyday commands

Try --list first when you are not sure what a word will select:

bash
actus test --list
text
running 5 tests (0 filtered)
geometry::area_does_not_overflow
geometry::area_of_six_by_seven
geometry::five_by_five_is_a_square
geometry::perimeter_of_six_by_seven
tests::area_through_the_facade

Then pick one test by its verb name or by its whole name. --exact accepts both:

bash
actus test --exact area_of_six_by_seven
text
running 1 test (4 filtered)
test geometry::area_of_six_by_seven ... ok (11 ms, exit code 0)
test result: ok. 1 passed; 0 failed; finished in 136 ms

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:

bash
actus test --file rect.act --list
actus test --module geometry --list
actus test --module tests --list

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:

bash
actus test --test-threads 1 of_six
text
running 2 tests (3 filtered)
test geometry::area_of_six_by_seven ... ok (11 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (11 ms, exit code 0)
test result: ok. 2 passed; 0 failed; finished in 169 ms

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:

bash
actus test --release of_six
text
running 2 tests (release) (3 filtered)
test geometry::area_of_six_by_seven ... ok (10 ms, exit code 0)
test geometry::perimeter_of_six_by_seven ... ok (10 ms, exit code 0)
test result: ok. 2 passed; 0 failed; finished in 32 ms

What you learned

  • A test is a verb with meta test in front of it. It has no parameters and returns an Int. 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 in tests/ and see only the open verbs of the modules they import. Neither folder has folders inside.
  • All files of one tests folder 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> or tests::<verb>. actus test builds each folder once and runs each test in its own process, in an empty folder.
  • actus test prints 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, --file and --module choose tests. --list shows what they select.

Next: Everyday commands.