Getting Started
Spawn has built-in support for tests. All tests in Spawn are written inside
files with a special suffix _test. For example, tests for the foo.sp file
are usually written in the foo_test.sp file.
Each file defines a set of tests that will be run with spawn --test. To define
a test, use the test keyword followed by the test name, enclosed in a string
literal:
By using string literals as test names, you can use spaces and special characters in test names and write test names in a more natural style.
Currently, each test file must start with a module definition main and import
all necessary modules for testing explicitly, even if the test is located in the
directory with the module under test.
At the moment, inside tests, it is possible to access private members of the module that are being tested.
Each test defines a set of assertions that must be met to pass the test. In the
example above, the test checks that the array arr after calling the push
method is equal to the expected value [1, 2, 3, 4]. If the condition is not
met, the test fails.
A special variable t with &mut testing.Tester type is defined in each test
and provides a set of assertions. The following assertions are available:
assert_eq(a, b, msg): checks thatais equal tobassert_ne(a, b, msg): checks thatais not equal tobassert_opt_eq(a, b, msg): checks that the value insideawith type?Tis equal tob, fails ifaisnoneassert_less(a, b, msg): checks thatais less thanbassert_greater(a, b, msg): checks thatais greater thanbassert_le(a, b, msg): checks thatais less than or equal tobassert_ge(a, b, msg): checks thatais greater than or equal tobassert_true(a, msg): checks thatais trueassert_false(a, msg): checks thatais falseassert_none(a, msg): checks thatais equal tononeassert_not_none(a, msg): checks thatais not equal tonone
The test can also be failed using the fail(msg) method:
Tests attributes
Each test can have attributes to change the behavior. For example, the #[skip]
attribute allows you to disable a test.
The following attributes are available:
#[skip]
A test marked with the #[skip] attribute will be skipped when running tests:
#[must_panic]
A test marked with the #[must_panic] attribute will only be considered
successful if a panic occurs within it. Otherwise, it will fail:
#[retry(N)]
A test marked with the #[retry(N)] attribute will be rerun N times if it
fails. If the test fails after N attempts, it will fail:
#[run_if(condition)]
A test marked with the #[run_if(condition)] attribute will only be run
if condition is true. The condition can be any compile time condition.
For example, you can define your compile time constant and use it as a condition:
Now, when running a test normally via spawn --test, the test will be skipped,
since is_ci is false by default. However, if you run the test
with -d is_ci=true, the test will run.
This attribute can also be used to run tests on a specific OS or architecture: