Unit Tests
Pyro has builtin support for running unit tests using the test
command.
You can view the helptext for the test
command by running:
pyro test --help
Specify a single file to test:
pyro test script.pyro
Specify multiple files to test:
pyro test *.pyro
Test Functions
For each input file specified on the command line, Pyro first executes the file, then runs any test functions it contains — i.e. functions whose name begins with $test_
.
- A test file passes if it executes without panicking.
- A test function passes if it executes without panicking.
- Test functions take no arguments.
You can use an assert
statement to make a test file or function panic if a test fails, e.g.
def add(a, b) { return a + b; } def $test_addition() { assert add(1, 2) == 3; # okay assert add(1, 2) == 4; # panics }
An assert
statement passes if its operand expression evaluates as truthy or fails (i.e. panics) if it evaluates as falsey.
(In Pyro, the values false
, null
, and err
are falsey; all other values are truthy.)
The syntax for an assert
statement is:
assert <expression> [, <error-message>] ;
Optionally, you can specify an error message in case the assertion fails, e.g.
let result = get_result(); assert result == 123, "unexpected result: ${result}";
Alternatively, you can make a test fail by calling the builtin $panic()
function directly, e.g.
def $test_result_type() { let result = get_result(123, 456); if !$is_i64(result) { $panic("expected i64, got {}", $type(result)); } }
Module-Level Tests
You don't need to use test functions for simple tests — you can put assert
statements at global-scope in a file, e.g.
assert "foo" + "bar" == "foobar";
The test
command will register the file as passing if it executes without panicking, or as failing if any of the assert
statements at global-scope fail.
Import Roots
When you run a script file using the test
command, Pyro uses the same default list of import roots it would use if you'd run the script directly —
i.e. the directory containing the script file will be added to the list of import roots, along with a modules
directory alongside the script file.
Use the -i/--import-root
option to add custom directories to the list of import roots.
Command Flags
-
Use the
-d/--debug
flag to isolate individual test failures. With this flag, execution will halt at the first panic and a full stack trace will be printed. -
Use the
-n/--no-color
flag for plaintext output without terminal colors. -
Use the
-v/--verbose
flag to show error messages.
Tutorial
You can find a unit-testing tutorial here.