feat: add support for suite before and after

`before` executes async code before all tests in the suite.
`after` executes async code after all tests in the suite.
This commit is contained in:
Eric Mastro 2022-02-24 22:15:45 +11:00
parent d2089a6182
commit 33946768c1
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
3 changed files with 42 additions and 3 deletions

View File

@ -33,12 +33,20 @@ proc someAsyncProc {.async.} =
suite "test async proc":
setupAll:
# invoke await in the suite setup:
await someAsyncProc()
teardownAll:
# invoke await in the suite teardown:
await someAsyncProc()
setup:
# invoke await in the test setup:
# invoke await in each test setup:
await someAsyncProc()
teardown:
# invoke await in the test teardown:
# invoke await in each test teardown:
await someAsyncProc()
test "async test":

View File

@ -1,6 +1,18 @@
template suite*(name, body) =
suite name:
# Runs before all tests in the suite
template setupAll(setupAllBody) {.used.} =
let b = proc {.async.} = setupAllBody
waitFor b()
# Runs after all tests in the suite
template teardownAll(teardownAllBody) {.used.} =
template teardownAllIMPL: untyped {.inject.} =
let a = proc {.async.} = teardownAllBody
waitFor a()
template setup(setupBody) {.used.} =
setup:
let asyncproc = proc {.async.} = setupBody
@ -11,7 +23,12 @@ template suite*(name, body) =
let asyncproc = proc {.async.} = teardownBody
waitFor asyncproc()
let suiteproc = proc = body # Avoids GcUnsafe2 warnings with chronos
let suiteproc = proc = # Avoids GcUnsafe2 warnings with chronos
body
when declared(teardownAllIMPL):
teardownAllIMPL()
suiteproc()
template test*(name, body) =

View File

@ -15,3 +15,17 @@ suite "test async proc":
test "async test":
# invoke await in tests:
await someAsyncProc()
suite "test async setupAll and teardownAll, allow multiple suites":
setupAll:
# invoke await in the test setup:
await someAsyncProc()
teardownAll:
# invoke await in the test teardown:
await someAsyncProc()
test "async test":
# invoke await in tests:
await someAsyncProc()