Pyro

A dynamically-typed, garbage-collected scripting language.

Version 0.19.2

Unit Testing

This tutorial demonstrates Pyro's builtin support for unit testing.



Pyro makes it easy to add unit tests to your code. You run unit tests using the test command.

Setup

Create a Pyro script to experiment with unit testing:

touch script.pyro

Add the following function that adds two values and returns the result:

def add(a, b) {
    return a + b;
}

Add a $main() function that calls the add() function:

def $main() {
    echo add(123, 456);
}

Try running the script:

pyro ./script.pyro

You should see the following output:

579

Testing valid input

Now let's add some tests for the add() function. Add the following test functions to script.pyro:

def $test_add_integers() {
    assert add(1, 2) == 3;
}

def $test_add_floats() {
    assert add(1.2, 2.3) == 3.5;
}

def $test_add_strings() {
    assert add("abc", "def") == "abcdef";
}

Run the tests using the test command:

pyro test ./script.pyro

You should see output confirming that all the tests have passed.

Testing invalid input

If you try adding incompatible types using the + operator — e.g. an integer and a string — the result will be a panic.

Assume we want our add() function to behave the same way. Let's add a test for this expected behaviour.

First, add a test that we expect to fail:

def $test_add_incompatible_types() {
    add(123, "foo");
}

Run the tests using the test command:

pyro test ./script.pyro

You should see output stating that the new test failed.

Try running the tests again, this time adding a -v/--verbose flag:

pyro test ./script.pyro -v

You should see similar output stating that the new test failed, but this time the output will include the error message for the panic.

We can test for an expected panic using a try expression. Replace the test function above with the following version:

def $test_add_incompatible_types() {
    assert $is_err(try add(123, "foo"));
}

Run the tests again using the test command:

pyro test ./script.pyro

You should see output confirming that all the tests have passed.

The test command

You can view the helptext for the test command by running:

pyro test --help

You can find more detailed documentation for the test command here.