@@ -228,52 +228,113 @@ To helps the management of the expected results, the option `--autosav` can be u
228
228
## Configuring TestSuites
229
229
230
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.
231
+ `before` and `after` annotations can be added to methods that must be called before/after each test case.
232
232
They can be used to factorize repetitive tasks:
233
233
234
- ~~~~
234
+ ~~~
235
235
class TestFoo
236
- test
236
+ test
237
237
238
- var subject: Foo
238
+ var subject: Foo is noinit
239
239
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
242
244
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
253
252
end
254
- ~~~~
253
+ ~~~
255
254
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.
257
256
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:
260
258
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
+ ~~~
262
283
module test_bdd_connector
284
+
263
285
import bdd_connector
286
+
264
287
# Testing the bdd_connector
265
288
class TestConnector
266
- # test cases using a server
289
+ test
290
+ # test cases using a server
267
291
end
292
+
268
293
# 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
271
296
end
297
+
272
298
# 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
275
301
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`
277
338
278
339
## Accessing the test suite environment
279
340
0 commit comments