Make API identical to std/unittest

Also includes a fix for GcUnsafe2 warnings with chronos.
This commit is contained in:
Mark Spanbroek 2021-01-11 17:28:55 +01:00
parent 195f8cf668
commit 737f01326c
4 changed files with 30 additions and 24 deletions

View File

@ -11,22 +11,20 @@ Use the [Nimble][2] package manager to add asynctest to an existing project.
Add the following to its .nimble file:
```nim
requires "asynctest >= 0.1.0 & < 0.2.0"
requires "asynctest >= 0.2.0 & < 0.3.0"
```
Usage
-----
Simply replace `test` with `asynctest` when you need to await asynchronous calls
in the test. The same holds for `asyncsetup` and `asyncteardown`, which allow
you to await asynchronous calls during test setup and teardown.
Simply replace `import unittest` with `import asynctest`, and you can await
asynchronous calls in tests, setup and teardown.
Example
-------
```nim
import unittest
import asynctest
import asyncdispatch # alternatively: import chronos
@ -35,15 +33,15 @@ proc someAsyncProc {.async.} =
suite "test async proc":
asyncsetup:
setup:
# invoke await in the test setup:
await someAsyncProc()
asyncteardown:
teardown:
# invoke await in the test teardown:
await someAsyncProc()
asynctest "some test":
test "async test":
# invoke await in tests:
await someAsyncProc()

View File

@ -1,14 +1,23 @@
template asynctest*(name, body) =
import unittest
export unittest except suite, test
template suite*(name, body) =
suite name:
template setup(setupBody) =
setup:
let asyncproc = proc {.async.} = setupBody
waitFor asyncproc()
template teardown(teardownBody) =
teardown:
let asyncproc = proc {.async.} = teardownBody
waitFor asyncproc()
let suiteproc = proc = body # Avoids GcUnsafe2 warnings with chronos
suiteproc()
template test*(name, body) =
test name:
let asyncproc = proc {.async.} = body
waitFor asyncproc()
template asyncsetup*(body) =
setup:
let asyncproc = proc {.async.} = body
waitFor asyncproc()
template asyncteardown*(body) =
teardown:
let asyncproc = proc {.async.} = body
waitFor asyncproc()

View File

@ -1,4 +1,4 @@
version = "0.1.0"
version = "0.2.0"
author = "asynctest Authors"
description = "Test asynchronous code"
license = "MIT"

View File

@ -1,4 +1,3 @@
import std/unittest
import std/asyncdispatch
import pkg/asynctest
@ -8,14 +7,14 @@ proc someAsyncProc {.async.} =
suite "test async proc":
asyncsetup:
setup:
# invoke await in the test setup:
await someAsyncProc()
asyncteardown:
teardown:
# invoke await in the test teardown:
await someAsyncProc()
asynctest "async test":
test "async test":
# invoke await in tests:
await someAsyncProc()