3 nitunit - executes the unit tests from Nit source files.
7 nitunit [*options*] FILE...
11 Unit testing in Nit can be achieved in two ways:
13 * using `DocUnits` in code comments or in markdown files
14 * using `TestSuites` with test unit files
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.
21 `DocUnits` can also be used in any markdown files.
23 `TestSuites` are test files coupled to a tested module.
24 They contain a list of test methods called TestCase.
26 ## Working with `DocUnits`
28 DocUnits are blocks of executable code placed in comments of modules, classes and properties.
29 The execution can be verified using `assert`.
36 # assert foo.bar == 10
42 Everything used in the test must be declared.
43 To test a method you have to instantiate its class:
48 # assert foo.bar == 10
51 # assert foo.baz(1, 2) == 3
52 fun baz(a, b: Int) do return a + b
56 In a single piece of documentation, each docunit is considered a part of a single module, thus regrouped when
58 Therefore, it is possible (and recommended) to split docunits in small parts if it make the explanation easier.
61 # Some example of grouped docunits
63 # Declare and initialize a variable `a`.
67 # So the value of `a` can be used
71 # even in complex operations
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).
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.
97 # var a: Int = someting
99 # if a == 1 then something else something-else
102 # Some code to not try to execute automatically
109 The `nitunit` command is used to test Nit files:
113 Groups (directories) can be given to test the documentation of the group and of all its Nit files:
117 Finally, standard markdown documents can be checked with:
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.
125 # NIT_TESTING is automatically set.
127 # assert "NIT_TESTING".environ == "true"
130 ## Working with `TestSuites`
132 TestSuites are Nit modules that define a set of TestCases.
134 A test suite is a module that uses the annotation `is test`.
136 It is common that a test suite focuses on testing a single module.
137 In this case, the name of the test suite is often `test_foo.nit` where `foo.nit` is the tested module.
139 The structure of a test suite is the following:
142 # test suite for module `foo`
143 module test_foo is test
145 import foo # can be intrude to test private things
150 # test case for `foo::Foo::baz`
152 var subject = new Foo
153 assert subject.baz(1, 2) == 3
158 Test suite can be executed using the same `nitunit` command:
162 `nitunit` will execute a test for each method with the `test` annotation in a class
163 also annotated with `test` so multiple tests can be executed for a single method:
170 var subject = new Foo
171 assert subject.baz(1, 2) == 3
174 var subject = new Foo
175 assert subject.baz(1, -2) == -1
182 Sometimes, it is easier to validate a `TestCase` by comparing its output with a text file containing the expected result.
184 For each TestCase `test_bar` of a TestSuite `test_mod.nit`, a corresponding file with the expected output is looked for:
186 * "test_mod.sav/test_bar.res". I.e. test-cases grouped by test-suites.
188 This is the default and is useful if there is a lot of test-suites and test-cases in a directory
190 * "sav/test_bar.res". I.e. all test-cases grouped in a common sub-directory.
192 Useful if there is a lot of test-suites OR test-cases in a directory.
194 * "test_bar.res" raw in the directory.
196 Useful is there is a few test-suites and test-cases in a directory.
198 All 3 are exclusive. If more than one exists, the test-case is failed.
200 If a corresponding file then the output of the test-case is compared with the file.
202 The `diff(1)` command is used to perform the comparison.
203 The test is failed if non-zero is returned by `diff`.
206 module test_mod is test
217 Where `test_mod.sav/test_bar.res` contains
223 If no corresponding `.res` file exists, then the output of the TestCase is ignored.
225 To helps the management of the expected results, the option `--autosav` can be used to automatically create and update them.
228 ## Configuring TestSuites
230 `TestSuite`s also provide annotations to configure the test run:
231 `before` and `after` annotations can be applied to methods that must be called before/after each test case.
232 They can be used to factorize repetitive tasks:
240 # Mandatory empty init
243 # Method executed before each test
244 fun set_up is before do
248 assert subject.baz(1, 2) == 3
251 assert subject.baz(1, -2) == -1
256 When using custom test attributes, an empty `init` must be declared to allow automatic test running.
258 `before_all` and `after_all` annotations can be applied to methods that must be called before/after each test suite.
259 They have to be declared at top level:
262 module test_bdd_connector
264 # Testing the bdd_connector
266 # test cases using a server
268 # Method executed before testing the module
270 # start server before all test cases
272 # Method executed after testing the module
274 # stop server after all test cases
278 ## Accessing the test suite environment
280 The `NIT_TESTING_PATH` environment variable contains the current test suite
282 Nitunit define this variable before the execution of each test suite.
283 It can be used to access files based on the current test suite location:
289 fun test_suite_path do
290 assert "NIT_TESTING_PATH".environ != ""
295 ## Generating test suites
297 Write test suites for big modules can be a repetitive and boring task...
298 To make it easier, `nitunit` can generate test skeletons for Nit modules:
300 $ nitunit --gen-suite foo.nit
302 This will generate the test suite `test_foo` containing test case stubs for all public
303 methods found in `foo.nit`.
309 Process also imported modules.
311 By default, only the modules indicated on the command line are tested.
313 With the `--full` option, all imported modules (even those in standard) are also precessed.
316 Output name (default is 'nitunit.xml').
318 `nitunit` produces a XML file compatible with JUnit.
321 Working directory (default is 'nitunit.out').
323 In order to execute the tests, nit files are generated then compiled and executed in the giver working directory.
325 In case of success, the directory is removed.
326 In case of failure, it is kept as is so files can be investigated.
329 nitc compiler to use.
331 By default, nitunit tries to locate the `nitc` program with the environment variable `NITC` or heuristics.
332 The option is used to indicate a specific nitc binary.
335 Does not compile and run tests.
337 ### `-p`, `--pattern`
338 Only run test case with name that match pattern.
340 Examples: `TestFoo`, `TestFoo*`, `TestFoo::test_foo`, `TestFoo::test_foo*`, `test_foo`, `test_foo*`
343 Automatically create/update .res files for black box testing.
345 If a black block test fails because a difference between the expected result and the current result then the expected result file is updated (and the test is passed).
347 If a test-case of a test-suite passes but that some output is generated, then an expected result file is created.
349 It is expected that the created/updated files are checked since the tests are considered passed.
350 A VCS like `git` is often a good tool to check the creation and modification of those files.
353 Disable time information in XML.
355 This is used to have reproducible XML results.
357 This option is automatically activated if `NIT_TESTING` is set.
362 Generate test suite skeleton for a module.
365 Force test generation even if file exists.
367 Any existing test suite will be overwritten.
370 Also generate test case for private methods.
373 Only display the skeleton, do not write any file.
376 # ENVIRONMENT VARIABLES
380 Indicate the specific Nit compiler executable to use. See `--nitc`.
384 The environment variable `NIT_TESTING` is set to `true` during the execution of program tests.
385 Some libraries of programs can use it to produce specific reproducible results; or just to exit their executions.
387 Unit-tests may unset this environment variable to retrieve the original behavior of such piece of software.
391 In order to maximize reproducibility, `SRAND` is set to 0.
392 This make the pseudo-random generator no random at all.
393 See `Sys::srand` for details.
395 To retrieve the randomness, unit-tests may unset this environment variable then call `srand`.
399 Parallel executions can cause some race collisions on named resources (e.g. DB table names).
400 To solve this issue, `NIT_TESTING_ID` is initialized with a distinct integer identifier that can be used to give unique names to resources.
402 Note: `rand` is not a recommended way to get a distinct identifier because its randomness is disabled by default. See `SRAND`.
404 ### `NIT_TESTING_PATH`
406 Only available for test suites.
407 Contains the module test suite path.
411 The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org>