nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / share / man / nitunit.md
1 # NAME
2
3 nitunit - executes the unit tests from Nit source files.
4
5 # SYNOPSIS
6
7 nitunit [*options*] FILE...
8
9 # DESCRIPTION
10
11 Unit testing in Nit can be achieved in two ways:
12
13 * using `DocUnits` in code comments or in markdown files
14 * using `TestSuites` with test unit files
15
16 `DocUnits` are executable pieces of code found in the documentation of groups, modules,
17 classes and properties.
18 They are used for documentation purpose, they should be kept simple and illustrative.
19 More advanced unit testing can be done using TestSuites.
20
21 `DocUnits` can also be used in any markdown files.
22
23 `TestSuites` are test files coupled to a tested module.
24 They contain a list of test methods called TestCase.
25
26 ## Working with `DocUnits`
27
28 DocUnits are blocks of executable code placed in comments of modules, classes and properties.
29 The execution can be verified using `assert`.
30
31 Example with a class:
32
33 ~~~
34 module foo
35 #    var foo = new Foo
36 #    assert foo.bar == 10
37 class Foo
38     var bar = 10
39 end
40 ~~~
41
42 Everything used in the test must be declared.
43 To test a method you have to instantiate its class:
44
45 ~~~
46 module foo
47 #    var foo = new Foo
48 #    assert foo.bar == 10
49 class Foo
50     #    var foo = new Foo
51     #    assert foo.baz(1, 2) == 3
52     fun baz(a, b: Int) do return a + b
53 end
54 ~~~
55
56 In a single piece of documentation, each docunit is considered a part of a single module, thus regrouped when
57 tested.
58 Therefore, it is possible (and recommended) to split docunits in small parts if it make the explanation easier.
59
60 ~~~~
61 # Some example of grouped docunits
62 #
63 # Declare and initialize a variable `a`.
64 #
65 #     var a = 1
66 #
67 # So the value of `a` can be used
68 #
69 #     assert a == 1
70 #
71 # even in complex operations
72 #
73 #     assert a + 1 == 2
74 fun foo do end
75 ~~~~
76
77 Sometime, some blocks of code has to be included in documentation but not considered by `nitunit`.
78 Those blocks are distinguished by their tagged fences (untagged fences or fences tagged `nit` are considered to be docunits).
79
80 ~~~~
81 # Some ASCII drawing
82 #
83 # ~~~~raw
84 #   @<
85 # <__)
86 # ~~~~
87 fun foo do end
88 ~~~~
89
90 The special fence-tag `nitish` could also be used to indicate pseudo-nit that will be ignored by nitunit but highlighted by nitdoc.
91 Such `nitish` piece of code can be used to enclose examples that cannot compile or that one do not want to be automatically executed.
92
93 ~~~~
94 # Some pseudo-nit
95 #
96 # ~~~~nitish
97 # var a: Int = someting
98 # # ...
99 # if a == 1 then something else something-else
100 # ~~~~
101 #
102 # Some code to not try to execute automatically
103 #
104 # ~~~~nitish
105 # system("rm -rf /")
106 # ~~~~
107 ~~~~
108
109 The `nitunit` command is used to test Nit files:
110
111     $ nitunit foo.nit
112
113 Groups (directories) can be given to test the documentation of the group and of all its Nit files:
114
115     $ nitunit lib/foo
116
117 Finally, standard markdown documents can be checked with:
118
119     $ nitunit foo.md
120
121 When testing, the environment variable `NIT_TESTING` is set to `true`.
122 This flag can be used by libraries and program to prevent (or limit) the execution of dangerous pieces of code.
123
124 ~~~~~
125 # NIT_TESTING is automatically set.
126 #
127 #     assert "NIT_TESTING".environ == "true"
128 ~~~~
129
130 ## Working with `TestSuites`
131
132 TestSuites are Nit files that define a set of TestCases for a particular module.
133
134 The test suite must be called `test_` followed by the name of the module to test.
135 So for the module `foo.nit` the test suite will be called `test_foo.nit`.
136
137 The structure of a test suite is the following:
138
139 ~~~~
140 # test suite for module `foo`
141 module test_foo
142 import foo # can be intrude to test private things
143 class TestFoo
144     # test case for `foo::Foo::baz`
145     fun test_baz do
146         var subject = new Foo
147         assert subject.baz(1, 2) == 3
148     end
149 end
150 ~~~~
151
152 Test suite can be executed using the same `nitunit` command:
153
154     $ nitunit foo.nit
155
156 `nitunit` will execute a test for each method named `test_*` in a class named `Test*`
157 so multiple tests can be executed for a single method:
158
159 ~~~~
160 class TestFoo
161     fun test_baz_1 do
162         var subject = new Foo
163         assert subject.baz(1, 2) == 3
164     end
165     fun test_baz_2 do
166         var subject = new Foo
167         assert subject.baz(1, -2) == -1
168     end
169 end
170 ~~~~
171
172 ## Black Box Testing
173
174 Sometimes, it is easier to validate a `TestCase` by comparing its output with a text file containing the expected result.
175
176 For each TestCase `test_bar` of a TestSuite `test_mod.nit`, if the corresponding file `test_mod.sav/test_bar.res` exists, then the output of the test is compared with the file.
177
178 The `diff(1)` command is used to perform the comparison.
179 The test is failed if non-zero is returned by `diff`.
180
181 ~~~
182 module test_mod is test_suite
183 class TestFoo
184         fun test_bar do
185                 print "Hello!"
186         end
187 end
188 ~~~
189
190 Where `test_mod.sav/test_bar.res` contains
191
192 ~~~raw
193 Hello!
194 ~~~
195
196 If no corresponding `.res` file exists, then the output of the TestCase is ignored.
197
198 ## Configuring TestSuites
199
200 `TestSuites` also provide methods to configure the test run:
201
202 `before_test` and `after_test`: methods called before/after each test case.
203 They can be used to factorize repetitive tasks:
204
205 ~~~~
206 class TestFoo
207     var subject: Foo
208     # Mandatory empty init
209     init do end
210     # Method executed before each test
211     fun before_test do
212         subject = new Foo
213     end
214     fun test_baz_1 do
215         assert subject.baz(1, 2) == 3
216     end
217     fun test_baz_2 do
218         assert subject.baz(1, -2) == -1
219     end
220 end
221 ~~~~
222
223 When using custom test attributes, an empty `init` must be declared to allow automatic test running.
224
225 `before_module` and `after_module`: methods called before/after each test suite.
226 They have to be declared at top level:
227
228 ~~~~
229 module test_bdd_connector
230 import bdd_connector
231 # Testing the bdd_connector
232 class TestConnector
233     # test cases using a server
234 end
235 # Method executed before testing the module
236 fun before_module do
237     # start server before all test cases
238 end
239 # Method executed after testing the module
240 fun after_module do
241     # stop server after all test cases
242 end
243 ~~~~
244
245 ## Generating test suites
246
247 Write test suites for big modules can be a repetitive and boring task...
248 To make it easier, `nitunit` can generate test skeletons for Nit modules:
249
250     $ nitunit --gen-suite foo.nit
251
252 This will generate the test suite `test_foo` containing test case stubs for all public
253 methods found in `foo.nit`.
254
255
256 # OPTIONS
257
258 ### `--full`
259 Process also imported modules.
260
261 By default, only the modules indicated on the command line are tested.
262
263 With the `--full` option, all imported modules (even those in standard) are also precessed.
264
265 ### `-o`, `--output`
266 Output name (default is 'nitunit.xml').
267
268 `nitunit` produces a XML file compatible with JUnit.
269
270 ### `--dir`
271 Working directory (default is '.nitunit').
272
273 In order to execute the tests, nit files are generated then compiled and executed in the giver working directory.
274
275 ### `--nitc`
276 nitc compiler to use.
277
278 By default, nitunit tries to locate the `nitc` program with the environment variable `NITC` or heuristics.
279 The option is used to indicate a specific nitc binary.
280
281 ### `--no-act`
282 Does not compile and run tests.
283
284 ### `-p`, `--pattern`
285 Only run test case with name that match pattern.
286
287 Examples: `TestFoo`, `TestFoo*`, `TestFoo::test_foo`, `TestFoo::test_foo*`, `test_foo`, `test_foo*`
288
289 ### `-t`, `--target-file`
290 Specify test suite location.
291
292 ## SUITE GENERATION
293
294 ### `--gen-suite`
295 Generate test suite skeleton for a module.
296
297 ### `-f`, `--force`
298 Force test generation even if file exists.
299
300 Any existing test suite will be overwritten.
301
302 ### `--private`
303 Also generate test case for private methods.
304
305 ### `--only-show`
306 Only display the skeleton, do not write any file.
307
308
309 # ENVIRONMENT VARIABLES
310
311 ### `NITC`
312
313 Indicate the specific Nit compiler executable to use. See `--nitc`.
314
315 ### `NIT_TESTING`
316
317 The environment variable `NIT_TESTING` is set to `true` during the execution of program tests.
318 Some libraries of programs can use it to produce specific reproducible results; or just to exit their executions.
319
320 Unit-tests may unset this environment variable to retrieve the original behavior of such piece of software.
321
322 ### `SRAND`
323
324 In order to maximize reproducibility, `SRAND` is set to 0.
325 This make the pseudo-random generator no random at all.
326 See `Sys::srand` for details.
327
328 To retrieve the randomness, unit-tests may unset this environment variable then call `srand`.
329
330 ### `NIT_TESTING_ID`
331
332 Parallel executions can cause some race collisions on named resources (e.g. DB table names).
333 To solve this issue, `NIT_TESTING_ID` is initialized with a distinct integer identifier that can be used to give unique names to resources.
334
335 Note: `rand` is not a recommended way to get a distinct identifier because its randomness is disabled by default. See `SRAND`.
336
337
338 # SEE ALSO
339
340 The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org>