Tests in VS Code

View source

Run meta test verbs from the VS Code Test Explorer or from the gutter of the editor. How tests are found, which command each button runs, and how the result is read.

The extension shows the meta test verbs of your workspace in the Test Explorer of VS Code. You run a test with the play button next to it, and VS Code marks it as passed or failed. Under the buttons the extension runs the same actus test as in the terminal. How to write a test, and where to put it, is in Testing.

A project with tests

This page uses a small project called stock. It has one module, stock, with one verb that is open. The facade of the module is src/stock/stock.act:

actus
open count;

The verb is in src/stock/count.act:

actus
open verb remaining(abs total: Int, abs sold: Int) -> Int {
    return total - sold;
}

And src/main.act uses it:

actus
import stock;

verb main() {
    println(int: stock::remaining(total: abs 10, sold: abs 4));
}
bash
actus run
text
6
process exited with status 0

The tests of a module go in the folder tests of the module. Create src/stock/tests/count.act:

actus
meta test
verb remaining_after_a_sale() -> Int {
    if remaining(total: abs 10, sold: abs 4) != 6 {
        return 1;
    }
    return 0;
}

meta test
verb nothing_left_when_all_sold() -> Int {
    if remaining(total: abs 5, sold: abs 5) != 0 {
        return 1;
    }
    return 0;
}

How tests are found

Open the Testing view in the Activity Bar. The extension has added a controller called Actus Tests. It groups the tests by project: the project of a test file is the nearest folder above it that holds an Actus.toml. Under each project there is one entry for each file that has tests, named with the path of the file inside the project, and under it one entry for each test. The project entry is named after its folder, or after its path inside the workspace folder when the project is not the workspace folder itself, so a workspace with several projects shows each one apart. For the project above, opened as the workspace folder stock:

text
Actus Tests
└── stock
    └── src/stock/tests/count.act
        ├── remaining_after_a_sale
        └── nothing_left_when_all_sold

The extension finds a test by reading the text of the .act files. It does not ask the compiler. A test is a meta test line followed by the first verb name( after it. Other meta lines in between, such as meta effect, are allowed. meta test and the verb may also be on one line. Text in a comment (# or //), in a string and in a """ documentation block is not read, so a meta test in a docstring is not a test. If a struct, enum, pack, role, perform, const, import, open or extern comes first, the meta test line before it is dropped.

The list stays up to date:

  • when you open a file, and as you type in it, from the text in the editor,
  • when a .act file is created, changed or deleted on disk,
  • when the workspace folders change,
  • when you press the refresh button of the Testing view.

The search through the workspace skips folders named target, node_modules and .git. A file with no tests is not listed.

Run tests

There is one run profile, called Run Actus Tests. It is the default. There is no debug profile.

  • The play button next to a test runs that test. The gutter of the editor has the same button on the line of the verb.
  • The play button next to a file runs all the tests of that file.
  • Run All Tests at the top runs every test, one file after another.

The extension starts actus test in the project folder of the test, the folder that holds its Actus.toml, and gives it the path of the file relative to that folder, so a test of a project nested inside the workspace runs in its own project. Tests of different projects run in separate commands. When exactly one test of a file is selected, it adds the name of the test and --exact. In the Test Results view, the first line of a run is the command, so you can see it. It ends with --profile and the value of actus.profile, debug by default. For a single test it is:

bash
actus test --file src/stock/tests/count.act remaining_after_a_sale --exact --profile debug
text
running 1 test (1 filtered)
test stock::remaining_after_a_sale ... ok (10 ms, exit code 0)
test result: ok. 1 passed; 0 failed; finished in 89 ms

When you select more than one test of the same file, or the file itself, the command has no name and no --exact, and the whole file runs. Only the tests you selected get a result. The extension has no way to run a part of a file other than one test.

Stopping a run takes effect before the next file starts. The process of the file that is running is not stopped.

Read the result

The extension reads the lines that actus test prints for each test:

text
test stock::remaining_after_a_sale ... ok (10 ms, exit code 0)

It takes the module path before the last ::, the verb name after it, the word ok or FAILED, and the time in milliseconds. The time shows next to the test. The Test Results view has the whole output, with ok in green and FAILED in red, and with test result: ok. or test result: FAILED. coloured in the same way.

Here is a third test in the file that cannot pass. It expects 4, and the verb gives 3:

actus
meta test
verb remaining_after_a_sale() -> Int {
    if remaining(total: abs 10, sold: abs 4) != 6 {
        return 1;
    }
    return 0;
}

meta test
verb nothing_left_when_all_sold() -> Int {
    if remaining(total: abs 5, sold: abs 5) != 0 {
        return 1;
    }
    return 0;
}

meta test
verb remaining_is_wrong_on_purpose() -> Int {
    if remaining(total: abs 5, sold: abs 2) != 4 {
        return 1;
    }
    return 0;
}

Running the file, from its play button, gives this:

bash
actus test --file src/stock/tests/count.act --profile debug
text
running 3 tests (0 filtered)
test stock::nothing_left_when_all_sold ... ok (10 ms, exit code 0)
test stock::remaining_after_a_sale ... ok (10 ms, exit code 0)
test stock::remaining_is_wrong_on_purpose ... FAILED (10 ms, exit code 1)
test result: FAILED. 2 passed; 1 failed; finished in 67 ms

In the Testing view the first two tests get a green check, and remaining_is_wrong_on_purpose gets a red cross. Its message is the text in the brackets of its line, 10 ms, exit code 1. A test that fails returns a number other than 0. If you want to know which check failed, return a different number for each one, as Testing says.

Some outcomes have no line for a test:

  • If the tests do not build, actus test prints an error and no test line. Every selected test fails, and its message is the text that actus test printed.
  • If a selected test has no ok line of its own, it is never counted as passed. When actus test ran no test at all (running 0 tests) or printed no line for the test, the test is marked as errored, and the message names the command, the folder it ran in and the lines that actus test printed.

The extension matches a result to a test by the name of the verb and the path of the module. It works out the module path from the path of the file, so src/stock/tests/count.act is the module stock and tests/count.act is the module tests. Two tests with the same verb name in two modules are told apart. If the module path does not fit any result, the extension takes the result only when exactly one result has that verb name.

A workspace that is not trusted runs nothing. The Test Results view says Actus tests require a trusted workspace.

Tests without the Test Explorer

The command Actus: Test runs actus test for the whole project and writes the output in the Actus Language Server channel of the Output panel. The task actus: test does the same in a terminal and fills the Problems panel when there is an error. See Commands, tasks and the status bar.

What you learned

  • The Testing view lists every meta test verb of the workspace, found from the text of the files, grouped by the project (the nearest Actus.toml).
  • A test, a file or Run All Tests runs actus test --file <path> in the project folder, with the name of the test and --exact when exactly one test of the file is chosen, and --profile <profile>.
  • The result of each test comes from its line test module::name ... ok or FAILED in the output; a test with no line of its own is errored, never passed.

Next: Commands, tasks and the status bar.