You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/testing.md
+15-5Lines changed: 15 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,15 +6,17 @@ Nushell provides a set of "assertion" commands in the standard library.
6
6
One could use built-in equality / order tests such as `==` or `<=` or more complex commands and throw errors manually when an expected condition fails, but using what the standard library has to offer is arguably easier!
7
7
8
8
In the following, it will be assumed that the `std assert` module has been imported inside the current scope
9
+
9
10
```nu
10
-
use stdassert
11
+
use std/assert
11
12
```
12
13
13
14
The foundation for every assertion is the `std assert` command. If the condition is not true, it makes an error.
14
15
15
16
```nu
16
17
assert (1 == 2)
17
18
```
19
+
18
20
```
19
21
Error:
20
22
× Assertion failed.
@@ -31,6 +33,7 @@ Optionally, a message can be set to show the intention of the assert command, wh
@@ -96,6 +101,7 @@ Then you'll have your detailed custom error message:
96
101
let $a = 13
97
102
assert even $a
98
103
```
104
+
99
105
```
100
106
Error:
101
107
× Assertion failed.
@@ -110,28 +116,29 @@ Error:
110
116
111
117
Now that we are able to write tests by calling commands from `std assert`, it would be great to be able to run them and see our tests fail when there is an issue and pass when everything is correct :)
112
118
113
-
114
119
### Nupm Package
115
120
116
121
In this first case, we will assume that the code you are trying to test is part of a [Nupm] package.
117
122
118
123
In that case, it is as easy as following the following steps
124
+
119
125
- create a `tests/` directory next to the `nupm.nuon` package file of your package
120
126
- make the `tests/` directory a valid module by adding a `mod.nu` file into it
121
127
- write commands inside `tests/`
122
128
- call `nupm test`
123
129
124
130
The convention is that any command fully exported from the `tests` module will be run as a test, e.g.
131
+
125
132
-`export def some-test` in `tests/mod.nu` will run
126
133
-`def just-an-internal-cmd` in `tests/mod.nu` will NOT run
127
134
-`export def another-test` in `tests/spam.nu` will run if and only if there is something like `export use spam.nu *` in `tests/mod.nu`
128
135
129
-
130
136
### Standalone Tests
131
137
132
138
If your Nushell script or module is not part of a [Nupm] package, the simplest way is to write tests in standalone scripts and then call them, either from a `Makefile` or in a CI:
133
139
134
140
Let's say we have a simple `math.nu` module which contains a simple Fibonacci command:
0 commit comments