2021-01-11 13:23:36 +00:00
|
|
|
asynctest
|
|
|
|
=========
|
|
|
|
|
|
|
|
Complements the standard [unittest][1] module in Nim to allow testing of
|
|
|
|
asynchronous code.
|
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
|
|
|
Use the [Nimble][2] package manager to add asynctest to an existing project.
|
|
|
|
Add the following to its .nimble file:
|
|
|
|
|
|
|
|
```nim
|
2024-08-14 08:11:51 +00:00
|
|
|
requires "asynctest >= 0.5.2 & < 0.6.0"
|
2021-01-11 13:23:36 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
2023-12-21 13:42:54 +00:00
|
|
|
Replace `import unittest` with one of the following imports, and you can await
|
2021-01-11 16:28:55 +00:00
|
|
|
asynchronous calls in tests, setup and teardown.
|
2021-01-11 13:23:36 +00:00
|
|
|
|
2023-12-21 13:42:54 +00:00
|
|
|
When you use Nim's standard library only ([asyncdispatch][4] and [unittest][1]):
|
|
|
|
```nim
|
|
|
|
import asynctest/asyncdispatch/unittest
|
|
|
|
```
|
|
|
|
|
|
|
|
When you use [chronos][5] or [unittest2][3], pick the import that matches your
|
|
|
|
choices:
|
|
|
|
|
|
|
|
```nim
|
|
|
|
import asynctest/asyncdispatch/unittest2 # standard async and unittest2
|
|
|
|
import asynctest/chronos/unittest # chronos and standard unittest
|
|
|
|
import asynctest/chronos/unittest2 # chronos and unittest2
|
|
|
|
```
|
|
|
|
|
2021-01-11 13:23:36 +00:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
```nim
|
|
|
|
|
2023-12-21 13:42:54 +00:00
|
|
|
import asynctest/asyncdispatch/unittest
|
2021-01-11 13:23:36 +00:00
|
|
|
|
|
|
|
proc someAsyncProc {.async.} =
|
|
|
|
# perform some async operations using await
|
|
|
|
|
|
|
|
suite "test async proc":
|
|
|
|
|
2021-01-11 16:28:55 +00:00
|
|
|
setup:
|
2022-02-24 11:15:45 +00:00
|
|
|
# invoke await in each test setup:
|
2021-01-11 13:23:36 +00:00
|
|
|
await someAsyncProc()
|
|
|
|
|
2021-01-11 16:28:55 +00:00
|
|
|
teardown:
|
2022-02-24 11:15:45 +00:00
|
|
|
# invoke await in each test teardown:
|
2021-01-11 13:23:36 +00:00
|
|
|
await someAsyncProc()
|
|
|
|
|
2021-01-11 16:28:55 +00:00
|
|
|
test "async test":
|
2021-01-11 13:23:36 +00:00
|
|
|
# invoke await in tests:
|
|
|
|
await someAsyncProc()
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2022-05-19 12:35:09 +00:00
|
|
|
check eventually
|
|
|
|
----------------
|
|
|
|
|
|
|
|
When you find yourself adding calls to `sleepAsync` to your tests, you might
|
|
|
|
want to consider using `check eventually` instead. It will repeatedly check
|
|
|
|
an expression until it becomes true. It has a built-in timeout of 5 seconds that
|
|
|
|
you can override.
|
|
|
|
|
|
|
|
```nim
|
|
|
|
var x: int
|
|
|
|
|
|
|
|
proc slowProcedure {.async.} =
|
|
|
|
# perform a slow operation
|
|
|
|
x = 42
|
|
|
|
|
|
|
|
let future = slowProcedure()
|
|
|
|
check eventually x == 42
|
|
|
|
await future
|
|
|
|
```
|
|
|
|
|
2023-12-21 13:42:54 +00:00
|
|
|
setupAll and teardownAll
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
The `setup` and `teardown` code runs before and after every test, just like the
|
|
|
|
standard [unittest][1] module. In addition we provide `setupAll` and
|
|
|
|
`teardownAll`. The `setupAll` code runs once before all tests in the suite, and
|
|
|
|
the `teardownAll` runs once after all tests in the suite. Use these only as a
|
|
|
|
last resort when setting up the test environment is very costly. Be careful that
|
|
|
|
the tests do not modify the environment that you set up, lest you introduce
|
|
|
|
dependencies between tests.
|
2021-07-07 08:32:50 +00:00
|
|
|
|
|
|
|
|
2021-01-11 13:23:36 +00:00
|
|
|
[1]: https://nim-lang.org/docs/unittest.html
|
|
|
|
[2]: https://github.com/nim-lang/nimble
|
2021-07-07 08:32:50 +00:00
|
|
|
[3]: https://github.com/status-im/nim-unittest2
|
2023-12-21 13:42:54 +00:00
|
|
|
[4]: https://nim-lang.org/docs/asyncdispatch.html
|
|
|
|
[5]: https://github.com/status-im/nim-chronos/
|