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 files that define a set of TestCases for a particular module.
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`.
137 The structure of a test suite is the following:
140 # test suite for module `foo`
142 import foo # can be intrude to test private things
144 # test case for `foo::Foo::baz`
146 var subject = new Foo
147 assert subject.baz(1, 2) == 3
152 Test suite can be executed using the same `nitunit` command:
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:
162 var subject = new Foo
163 assert subject.baz(1, 2) == 3
166 var subject = new Foo
167 assert subject.baz(1, -2) == -1
172 `TestSuites` also provide methods to configure the test run:
174 `before_test` and `after_test`: methods called before/after each test case.
175 They can be used to factorize repetitive tasks:
180 # Mandatory empty init
182 # Method executed before each test
187 assert subject.baz(1, 2) == 3
190 assert subject.baz(1, -2) == -1
195 When using custom test attributes, an empty `init` must be declared to allow automatic test running.
197 `before_module` and `after_module`: methods called before/after each test suite.
198 They have to be declared at top level:
201 module test_bdd_connector
203 # Testing the bdd_connector
205 # test cases using a server
207 # Method executed before testing the module
209 # start server before all test cases
211 # Method executed after testing the module
213 # stop server after all test cases
217 ## Generating test suites
219 Write test suites for big modules can be a repetitive and boring task...
220 To make it easier, `nitunit` can generate test skeletons for Nit modules:
222 $ nitunit --gen-suite foo.nit
224 This will generate the test suite `test_foo` containing test case stubs for all public
225 methods found in `foo.nit`.
231 Process also imported modules.
233 By default, only the modules indicated on the command line are tested.
235 With the `--full` option, all imported modules (even those in standard) are also precessed.
238 Output name (default is 'nitunit.xml').
240 ### `nitunit` produces a XML file comatible with JUnit.
243 Working directory (default is '.nitunit').
245 In order to execute the tests, nit files are generated then compiled and executed in the giver working directory.
248 Does not compile and run tests.
250 ### `-p`, `--pattern`
251 Only run test case with name that match pattern.
253 Examples: `TestFoo`, `TestFoo*`, `TestFoo::test_foo`, `TestFoo::test_foo*`, `test_foo`, `test_foo*`
255 ### `-t`, `--target-file`
256 Specify test suite location.
261 Generate test suite skeleton for a module.
264 Force test generation even if file exists.
266 Any existing test suite will be overwritten.
269 Also generate test case for private methods.
272 Only display the skeleton, do not write any file.
276 The Nit language documentation and the source code of its tools and libraries may be downloaded from <http://nitlanguage.org>