File size: 4,903 Bytes
8d756d3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# Testing your Nushell code
The [standard library](standard_library.md) has a unit testing framework to ensure that your code works as expected.
## Quick start
Have a file, called `test_math.nu`:
```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:
```nu
β― 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:
```nu
β― 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:
```nu
β― 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:
```nu
β― 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`:
```nu
β― 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`](/commands/docs/error_make.md) for the `assert` command:
```nu
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:
```nu
β― 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](https://github.com/nushell/nushell/blob/main/crates/nu-std/tests/).
## 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:
```nu
β― std run-tests
β― NU_LOG_LEVEL=DEBUG std run-tests
β― NU_LOG_LEVEL=WARNING std run-tests
```
|