add suiteTeardown() template

This commit is contained in:
Ștefan Talpalaru 2019-05-28 23:58:13 +02:00
parent b8ab4657e4
commit d01ed926d7
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
2 changed files with 16 additions and 4 deletions

View File

@ -116,6 +116,11 @@ suite "suite #2":
sleep(100) sleep(100)
check 1 == 1 check 1 == 1
suiteTeardown:
echo "suite teardown"
echo "this will be shown first"
# go back to the default one # go back to the default one
clearOutputFormatters() clearOutputFormatters()

View File

@ -106,7 +106,8 @@
## expect(IndexError): ## expect(IndexError):
## discard v[4] ## discard v[4]
## ##
## echo "suite teardown: run once after the tests" ## suiteTeardown:
## echo "suite teardown: run once after the tests"
import locks, macros, sets, strutils, streams, times import locks, macros, sets, strutils, streams, times
@ -133,7 +134,7 @@ when paralleliseTests:
# (`flowVars` will be initialized in each child thread, when using nested tests, by the compiler) # (`flowVars` will be initialized in each child thread, when using nested tests, by the compiler)
# TODO: try getting rid of them when nim-0.20.0 is released # TODO: try getting rid of them when nim-0.20.0 is released
var flowVars {.threadvar.}: seq[FlowVarBase] var flowVars {.threadvar.}: seq[FlowVarBase]
proc repeatableSync() = proc repeatableSync*() =
sync() sync()
for flowVar in flowVars: for flowVar in flowVars:
blockUntil(flowVar) blockUntil(flowVar)
@ -540,12 +541,20 @@ template suite*(name, body) {.dirty.} =
var testTeardownIMPLFlag {.used.} = true var testTeardownIMPLFlag {.used.} = true
template testTeardownIMPL: untyped {.dirty.} = teardownBody template testTeardownIMPL: untyped {.dirty.} = teardownBody
template suiteTeardown(suiteTeardownBody: untyped) {.dirty, used.} =
var testSuiteTeardownIMPLFlag {.used.} = true
template testSuiteTeardownIMPL: untyped {.dirty.} = suiteTeardownBody
let testSuiteName {.used.} = name let testSuiteName {.used.} = name
ensureInitialized() ensureInitialized()
try: try:
suiteStarted(name) suiteStarted(name)
body body
when declared(testSuiteTeardownIMPLFlag):
when paralleliseTests:
repeatableSync()
testSuiteTeardownIMPL()
finally: finally:
suiteEnded() suiteEnded()
@ -605,8 +614,6 @@ template test*(name, body) =
) )
testEnded(testResult) testEnded(testResult)
checkpoints = @[] checkpoints = @[]
# when running tests in parallel, "formatters" manipulation may occur in
# teardown(), so it needs to be after testEnded()
when declared(testTeardownIMPLFlag): when declared(testTeardownIMPLFlag):
testTeardownIMPL() testTeardownIMPL()