Skip to content
This repository was archived by the owner on Dec 23, 2022. It is now read-only.

Commit e3b9ec3

Browse files
committed
nitunit: update documentation
Signed-off-by: Alexandre Terrasa <[email protected]>
1 parent 4bb45df commit e3b9ec3

File tree

2 files changed

+161
-41
lines changed

2 files changed

+161
-41
lines changed

share/man/nitunit.md

+88-27
Original file line numberDiff line numberDiff line change
@@ -228,52 +228,113 @@ To helps the management of the expected results, the option `--autosav` can be u
228228
## Configuring TestSuites
229229
230230
`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.
231+
`before` and `after` annotations can be added to methods that must be called before/after each test case.
232232
They can be used to factorize repetitive tasks:
233233
234-
~~~~
234+
~~~
235235
class TestFoo
236-
test
236+
test
237237
238-
var subject: Foo
238+
var subject: Foo is noinit
239239
240-
# Mandatory empty init
241-
init do end
240+
# Method executed before each test
241+
fun set_up is before do
242+
subject = new Foo
243+
end
242244
243-
# Method executed before each test
244-
fun set_up is before do
245-
subject = new Foo
246-
end
247-
fun baz_1 is test do
248-
assert subject.baz(1, 2) == 3
249-
end
250-
fun baz_2 is test do
251-
assert subject.baz(1, -2) == -1
252-
end
245+
fun baz_1 is test do
246+
assert subject.baz(1, 2) == 3
247+
end
248+
249+
fun baz_2 is test do
250+
assert subject.baz(1, -2) == -1
251+
end
253252
end
254-
~~~~
253+
~~~
255254
256-
When using custom test attributes, an empty `init` must be declared to allow automatic test running.
255+
When using custom test attributes, a empty init must be declared to allow automatic test running.
257256
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:
257+
At class level, `before_all` and `after_all` annotations can be set on methods that must be called before/after all the test cases in the class:
260258
261-
~~~~
259+
~~~
260+
class TestFoo
261+
test
262+
263+
var subject: Foo is noinit
264+
265+
# Method executed before all tests in the class
266+
fun set_up is before_all do
267+
subject = new Foo
268+
end
269+
270+
fun baz_1 is test do
271+
assert subject.baz(1, 2) == 3
272+
end
273+
274+
fun baz_2 is test do
275+
assert subject.baz(1, -2) == -1
276+
end
277+
end
278+
~~~
279+
280+
`before_all` and `after_all` annotations can also be set on methods that must be called before/after each test suite when declared at top level:
281+
282+
~~~
262283
module test_bdd_connector
284+
263285
import bdd_connector
286+
264287
# Testing the bdd_connector
265288
class TestConnector
266-
# test cases using a server
289+
test
290+
# test cases using a server
267291
end
292+
268293
# Method executed before testing the module
269-
fun before_module do
270-
# start server before all test cases
294+
fun setup_db is before_all do
295+
# start server before all test cases
271296
end
297+
272298
# Method executed after testing the module
273-
fun after_module do
274-
# stop server after all test cases
299+
fun teardown_db is after_all do
300+
# stop server after all test cases
275301
end
276-
~~~~
302+
~~~
303+
304+
When dealing with multiple test suites, niunit allows you to import other test suites to factorize your tests:
305+
306+
~~~
307+
module test_bdd_users
308+
309+
import test_bdd_connector
310+
311+
# Testing the user table
312+
class TestUsersTable
313+
test
314+
# test cases using the db server from `test_bdd_connector`
315+
end
316+
317+
fun setup_table is before_all do
318+
# create user table
319+
end
320+
321+
fun teardown_table is after_all do
322+
# drop user table
323+
end
324+
~~~
325+
326+
Methods with `before*` and `after*` annotations are linearized and called in different ways.
327+
328+
* `before*` methods are called from the least specific to the most specific
329+
* `after*` methods are called from the most specific to the least specific
330+
331+
In the previous example, the execution order would be:
332+
333+
1. `test_bdd_connector::setup_db`
334+
2. `test_bdd_users::setup_table`
335+
3. `all test cases from test_bdd_users`
336+
4. `test_bdd_users::teardown_table`
337+
5. `test_bdd_connector::teardown_db`
277338
278339
## Accessing the test suite environment
279340

src/testing/README.md

+73-14
Original file line numberDiff line numberDiff line change
@@ -133,28 +133,87 @@ end
133133

134134
When using custom test attributes, a empty init must be declared to allow automatic test running.
135135

136-
`before_all` and `after_all` annotations can be set on methods that must be called before/after each test suite.
137-
They have to be declared at top level:
136+
At class level, `before_all` and `after_all` annotations can be set on methods that must be called before/after all the test cases in the class:
138137

139-
module test_bdd_connector
138+
~~~nitish
139+
class TestFoo
140+
test
140141
141-
import bdd_connector
142+
var subject: Foo is noinit
142143
143-
# Testing the bdd_connector
144-
class TestConnector
145-
test
146-
# test cases using a server
144+
# Method executed before all tests in the class
145+
fun set_up is before_all do
146+
subject = new Foo
147147
end
148148
149-
# Method executed before testing the module
150-
fun before_module is before_all do
151-
# start server before all test cases
149+
fun baz_1 is test do
150+
assert subject.baz(1, 2) == 3
152151
end
153152
154-
# Method executed after testing the module
155-
fun after_module is after_all do
156-
# stop server after all test cases
153+
fun baz_2 is test do
154+
assert subject.baz(1, -2) == -1
157155
end
156+
end
157+
~~~
158+
159+
`before_all` and `after_all` annotations can also be set on methods that must be called before/after each test suite when declared at top level:
160+
161+
~~~nitish
162+
module test_bdd_connector
163+
164+
import bdd_connector
165+
166+
# Testing the bdd_connector
167+
class TestConnector
168+
test
169+
# test cases using a server
170+
end
171+
172+
# Method executed before testing the module
173+
fun setup_db is before_all do
174+
# start server before all test cases
175+
end
176+
177+
# Method executed after testing the module
178+
fun teardown_db is after_all do
179+
# stop server after all test cases
180+
end
181+
~~~
182+
183+
When dealing with multiple test suites, niunit allows you to import other test suites to factorize your tests:
184+
185+
~~~nitish
186+
module test_bdd_users
187+
188+
import test_bdd_connector
189+
190+
# Testing the user table
191+
class TestUsersTable
192+
test
193+
# test cases using the db server from `test_bdd_connector`
194+
end
195+
196+
fun setup_table is before_all do
197+
# create user table
198+
end
199+
200+
fun teardown_table is after_all do
201+
# drop user table
202+
end
203+
~~~
204+
205+
Methods with `before*` and `after*` annotations are linearized and called in different ways.
206+
207+
* `before*` methods are called from the least specific to the most specific
208+
* `after*` methods are called from the most specific to the least specific
209+
210+
In the previous example, the execution order would be:
211+
212+
1. `test_bdd_connector::setup_db`
213+
2. `test_bdd_users::setup_table`
214+
3. `all test cases from test_bdd_users`
215+
4. `test_bdd_users::teardown_table`
216+
5. `test_bdd_connector::teardown_db`
158217

159218
## Generating test suites
160219

0 commit comments

Comments
 (0)