Testing your Nushell code
The standard library has a unit testing framework to ensure that your code works as expected.
Quick start
Have a file, called test_math.nu
:
use std assert
#[test]
def test_addition [] {
assert equal (1 + 2) 3
}
#[test]
def test_skip [] {
assert skip
}
#[test]
def test_failing [] {
assert false "This is just for testing"
}
Run the tests:
β― use std testing run-tests
β― run-tests
INF|2023-04-12T10:42:29.099|Running tests in test_math
Error:
Γ This is just for testing
ββ[C:\wip\test_math.nu:13:1]
13 β def test_failing [] {
14 β assert false "This is just for testing"
Β· βββ¬ββ
Β· β°ββ It is not true.
15 β }
β°ββββ
WRN|2023-04-12T10:42:31.086|Test case test_skip is skipped
Error:
Γ some tests did not pass (see complete errors above):
β
β test_math test_addition
β β¨― test_math test_failing
β s test_math test_skip
β
Assert commands
The foundation for every assertion is the std assert
command. If the condition is not true, it makes an error. For example:
β― std assert (1 == 2)
Error:
Γ Assertion failed.
ββ[entry #13:1:1]
1 β std assert (1 == 2)
Β· ββββ¬ββ
Β· β°ββ It is not true.
β°ββββ
Optionally, a message can be set to show the intention of the assert command, what went wrong or what was expected:
β― std assert ($a == 19) $"The lockout code is wrong, received: ($a)"
Error:
Γ The lockout code is wrong, received: 13
ββ[entry #25:1:1]
1 β std assert ($a == 19) $"The lockout code is wrong, received: ($a)"
Β· βββββ¬βββ
Β· β°ββ It is not true.
β°ββββ
There are many assert commands, which behave exactly as the base one with the proper operator. The additional value for them is the ability for better error messages.
For example this is not so helpful without additional message:
β― std assert ($b | str contains $a)
Error:
Γ Assertion failed.
ββ[entry #35:1:1]
1 β assert ($b | str contains $a)
Β· βββββββ¬βββββ
Β· β°ββ It is not true.
β°ββββ
While with using assert str contains
:
β― std assert str contains $b $a
Error:
Γ Assertion failed.
ββ[entry #34:1:1]
1 β assert str contains $b $a
Β· βββ¬ββ
Β· β°ββ 'haystack' does not contain 'a needle'.
β°ββββ
In general for base assert
command it is encouraged to always provide the additional message to show what went wrong. If you cannot use any built-in assert command, you can create a custom one with passing the label for error make
for the assert
command:
def "assert even" [number: int] {
std assert ($number mod 2 == 0) --error-label {
start: (metadata $number).span.start,
end: (metadata $number).span.end,
text: $"($number) is not an even number",
}
}
Then you'll have your detailed custom error message:
β― let $a = 13
β― assert even $a
Error:
Γ Assertion failed.
ββ[entry #37:1:1]
1 β assert even $a
Β· ββ¬
Β· β°ββ 13 is not an even number
β°ββββ
Test modules & test cases
The naming convention for test modules is test_<your_module>.nu
and test_<test name>
for test cases.
In order for a function to be recognized as a test by the test runner it needs to be annotated with #[test]
.
The following annotations are supported by the test runner:
- test - test case to be executed during test run
- test-skip - test case to be skipped during test run
- before-all - function to run at the beginning of test run. Returns a global context record that is piped into every test function
- before-each - function to run before every test case. Returns a per-test context record that is merged with global context and piped into test functions
- after-each - function to run after every test case. Receives the context record just like the test cases
- after-all - function to run after all test cases have been executed. Receives the global context record
The standard library itself is tested with this framework, so you can find many examples in the Nushell repository.
Setting verbosity
The unit testing framework uses the log
commands from the standard library to display information, so you can set NU_LOG_LEVEL
if you want more or less details:
β― std run-tests
β― NU_LOG_LEVEL=DEBUG std run-tests
β― NU_LOG_LEVEL=WARNING std run-tests