-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-syntax.jade
200 lines (150 loc) · 5.95 KB
/
test-syntax.jade
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: Test Syntax
description: A breakdown of ZUnit's test syntax and helper methods
seq: 0
---
extends /layouts/_docs
block docs-content
section
:marked
Tests in ZUnit have a simple syntax, which is inspired by the [BATS](https://github.com/sstephenson/bats) framework. You can add as many tests as you like to each test file, and the body of each test can contain any valid ZSH code.
There are only two rules you need to remember whilst writing your tests:
1. Test names must be unique within a file.
2. The shebang `#!/usr/bin/env zunit` **MUST** appear at the top of each test file, or ZUnit will not run it.
feature
:marked
```zunit
#!/usr/bin/env zunit
@test 'My first test' {
# Test contents here
}
```
section
:marked
### Making Assertions
ZUnit comes with a powerful assertion library to aid you in writing your tests. The `assert` helper allows you to make assertions, which fail your test automatically if they are incorrect.
To see the full range of assertion methods available to you, take a look at their [documentation](#{baseUrl}/docs/writing-tests/assertions).
By default, tests which do not contain any assertions will be marked as risky, and result in an overall failure of the test run. You can disable this functionality by passing ZUnit the `--allow-risky` option.
feature.bump
:marked
```zunit
#!/usr/bin/env zunit
@test 'Testing assertions' {
assert 1 equals 1
}
```
section
:marked
### Loading scripts
Each of your tests is run in isolation, meaning that there is no variable or function leakage between tests. The `load` helper function will source a script into the test environment for you, allowing you to set up variables and functions etc.
feature.bump
:marked
```zunit
### In my-script.zsh:
export testing='Tada!'
### In tests/my-script.zunit:
@test 'Test loading scripts' {
load ../my-script
# The $testing variable is now set here
assert "$testing" same_as 'Tada!'
}
```
section
:marked
You can load any absolute or relative file path, and for files ending in `.zsh` including the extension is optional.
feature
:marked
```zunit
# In the example above, the following are all equivalent
load ../my-script
load ../my-script.zsh
load /path/to/project/my-script
load /path/to/project/my-script.zsh
```
section
:marked
### Running commands
You can run commands within your tests using the `run` helper. Doing this stops a failing command from ending your test early, and allows you to make assertions on its exit status and output.
The command's exit code is stored in the `$state` variable.
The full output of the command is stored in `$output`.
Each line of the output is also stored in the `$lines` array, allowing you to run assertions against individual lines.
The command can use complex constructs like `;`, `|`, `||`, `&&`, redirections, etc. but they should be quoted.
feature.bump
:marked
```zunit
@test 'Test command output' {
# Run the command, including arguments
run ls ~/my-dir \| head -n 3
assert $state equals 0
assert "$output" is_not_empty
assert "${lines[3]}" equals 'my-third-file'
}
```
section
:marked
### Evaluating code
With the use of the eval-like `evl` helper you can run e.g. functions and test for various side-effects, like a value of a global parameter. So it not only allows you to make assertions on its exit status and output, but also on the changed state of Zshell.
Like with the `run` helper, the command's exit code is stored in the `$state` variable.
The full output of the command is stored in `$output`.
Each line of the output is also stored in the `$lines` array, allowing you to run assertions against individual lines.
The command can use complex constructs like `;`, `|`, `||`, `&&`, redirections, etc. but they should be quoted.
feature.bump
:marked
```zunit
@test 'Test command output' {
# Run the function through eval
# It returns in $reply array
evl a-function-returning-in-reply arg1 arg2
assert $state equals 0
assert "${reply[1]}" equals 'my-answer-1'
assert "${reply[2]}" equals 'my-answer-2'
}
```
section
:marked
### Skipping tests
If you want to skip a test, just use the `skip` helper method. This will stop execution of the test immediately and output the provided message, without failing the whole test run.
feature.bump
:marked
```zunit
# The test will be skipped if it is run on macOS
@test 'Test command output' {
if [[ $OSTYPE =~ 'darwin.*' ]]; then
skip 'Test does not run on macOS'
fi
# Test code here
}
```
section
:marked
### Pass a test manually
If you want to pass a test manually, use the `pass` helper method. This will stop execution of the test immediately, and mark it as passed.
feature.bump
:marked
```zunit
@test 'Test will pass' {
pass
}
```
section
:marked
### Fail a test manually
If you want to fail a test manually, use the `fail` helper method. This will stop execution of the test immediately, mark it as failed, and print the message passed to it.
feature.bump
:marked
```zunit
@test 'Test will fail' {
fail 'Failed'
}
```
section
:marked
### Throw an error
If you want to throw an error manually, use the `error` helper method. This will stop execution of the test immediately, mark it as errored, and print the error message passed to it.
feature.bump
:marked
```zunit
@test 'Test will error' {
error 'Something went wrong'
}
```