Skip to main content

Overview

To write unit tests for Rell code, use test module. You need to use the @test annotation to define a test module.

@test module;

function test_foo() {
assert_equals(2 + 2, 4);
}

function test_bar() {
assert_equals(2 + 2, 5);
}

All functions in a test module that start with test_ (and a function called exactly test) are test functions that're executed when you run the test module.

If the module has the "_test" suffix, it becomes a test module for the module that bears the same name without the suffix. For example, if the module name is program, the test module is program_test.

To run a test module, use the test command (chr test):

chr test --settings chromia.yml --modules my_test_module

For more information, see the chr test topic. Each test function gets executed independently of others, and a summary gets printed at the end:

TEST RESULTS:

my_test_module:test_foo OK
my_test_module:test_bar FAILED

SUMMARY: 1 FAILED / 1 PASSED / 2 TOTAL


***** FAILED *****

Test module (file data_test.rell):

@test module;
import data;

function test_add_user() {
assert_equals(data.user @* {}(.name), list<text>());

val tx = rell.test.tx(data.add_user('Bob'));
assert_equals(data.user @* {}(.name), list<text>());

tx.run();
assert_equals(data.user @* {}(.name), ['Bob']);
}

Example of a test module

Here's an example of a test module:

@test module;
import main;

function test_init(){

assert_equals((main.user @* {}(.username)).size(),0);
assert_equals((main.balance @* {}(.user)).size(),0);
rell.test.tx().op(main.init(rell.test.pubkeys.alice)).run();
assert_equals(main.user @* {}(.username).size(),1);
assert_equals(main.balance@{.user == main.user @ { .pubkey == rell.test.pubkeys.alice}}(.amount),1000000);

}

function test_register_user(){

rell.test.tx().op(main.init(rell.test.pubkeys.alice)).run();
assert_equals(main.user @* {}(.username).size(),1);
rell.test.tx().op(main.register_user(rell.test.pubkeys.alice,rell.test.pubkeys.bob,"bob",100)).sign(rell.test.keypairs.alice).run();
assert_equals(main.user @* {}(.username).size(),2);
assert_equals(main.user @ {.pubkey == rell.test.pubkeys.bob}(.username),"bob");
assert_equals(main.balance@{.user == main.user @ {.pubkey == rell.test.pubkeys.bob}}(.amount),100);
}

function test_blocks(){

val tx1 = rell.test.tx().op(main.init(rell.test.pubkeys.alice));
val tx2 = rell.test.tx().op(main.register_user(rell.test.pubkeys.alice,rell.test.pubkeys.bob,"bob",100)).sign(rell.test.keypairs.alice);
val tx3 = rell.test.tx().op(main.create_channel(rell.test.pubkeys.alice,"channel 1")).sign(rell.test.keypairs.alice);
rell.test.block().tx(tx1).tx(tx2).tx(tx3).run();
val tx4 = rell.test.tx().op(main.add_channel_member(rell.test.pubkeys.alice,"channel 1","bob")).sign(rell.test.keypairs.alice);
rell.test.block().tx(tx4).run();
}

Unit tests also display the duration of each test case and the overall test suite duration.

    TEST RESULTS:

OK tests.foobar:test_foo (0.005s)

FAILED tests.foobar:test_fail_require (0.001s)
FAILED tests.foobar:test_fail_assert_equals (0.001s)

SUMMARY: 2 FAILED / 1 PASSED / 3 TOTAL (0.007s)