Implement ability to run a single test using unittest module.
This commit is contained in:
parent
4bf6325739
commit
838be26f53
1 changed files with 38 additions and 17 deletions
|
|
@ -13,7 +13,24 @@
|
||||||
##
|
##
|
||||||
## The test status and name is printed after any output or traceback.
|
## The test status and name is printed after any output or traceback.
|
||||||
##
|
##
|
||||||
## Example:
|
## Tests can be nested, however failure of a nested test will not mark the
|
||||||
|
## parent test as failed. Setup and teardown are inherited. Setup can be
|
||||||
|
## overridden locally.
|
||||||
|
##
|
||||||
|
## Compiled test files return the number of failed test as exit code, while
|
||||||
|
## ``nim c -r <testfile.nim>`` exits with 0 or 1
|
||||||
|
##
|
||||||
|
## Running a single test
|
||||||
|
## ---------------------
|
||||||
|
##
|
||||||
|
## Simply specify the test name as a command line argument.
|
||||||
|
##
|
||||||
|
## .. code::
|
||||||
|
##
|
||||||
|
## nim c -r test "my super awesome test name"
|
||||||
|
##
|
||||||
|
## Example
|
||||||
|
## -------
|
||||||
##
|
##
|
||||||
## .. code:: nim
|
## .. code:: nim
|
||||||
##
|
##
|
||||||
|
|
@ -42,16 +59,9 @@
|
||||||
## discard v[4]
|
## discard v[4]
|
||||||
##
|
##
|
||||||
## echo "suite teardown: run once after the tests"
|
## echo "suite teardown: run once after the tests"
|
||||||
##
|
|
||||||
##
|
|
||||||
## Tests can be nested, however failure of a nested test will not mark the
|
|
||||||
## parent test as failed. Setup and teardown are inherited. Setup can be
|
|
||||||
## overridden locally.
|
|
||||||
## Compiled test files return the number of failed test as exit code, while
|
|
||||||
## nim c -r <testfile.nim> exits with 0 or 1
|
|
||||||
|
|
||||||
import
|
import
|
||||||
macros, strutils, streams, times
|
macros, strutils, streams, times, sets
|
||||||
|
|
||||||
when declared(stdout):
|
when declared(stdout):
|
||||||
import os
|
import os
|
||||||
|
|
@ -111,6 +121,7 @@ var
|
||||||
|
|
||||||
checkpoints {.threadvar.}: seq[string]
|
checkpoints {.threadvar.}: seq[string]
|
||||||
formatters {.threadvar.}: seq[OutputFormatter]
|
formatters {.threadvar.}: seq[OutputFormatter]
|
||||||
|
testsToRun {.threadvar.}: HashSet[string]
|
||||||
|
|
||||||
when declared(stdout):
|
when declared(stdout):
|
||||||
abortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
|
abortOnError = existsEnv("NIMTEST_ABORT_ON_ERROR")
|
||||||
|
|
@ -290,12 +301,22 @@ method suiteEnded*(formatter: JUnitOutputFormatter) =
|
||||||
formatter.stream.writeLine("\t</testsuite>")
|
formatter.stream.writeLine("\t</testsuite>")
|
||||||
|
|
||||||
proc shouldRun(testName: string): bool =
|
proc shouldRun(testName: string): bool =
|
||||||
result = true
|
if testsToRun.len == 0:
|
||||||
|
return true
|
||||||
|
|
||||||
proc ensureFormattersInitialized() =
|
result = testName in testsToRun
|
||||||
|
|
||||||
|
proc ensureInitialized() =
|
||||||
if formatters == nil:
|
if formatters == nil:
|
||||||
formatters = @[OutputFormatter(defaultConsoleFormatter())]
|
formatters = @[OutputFormatter(defaultConsoleFormatter())]
|
||||||
|
|
||||||
|
if not testsToRun.isValid:
|
||||||
|
testsToRun.init()
|
||||||
|
when declared(os):
|
||||||
|
# Read tests to run from the command line.
|
||||||
|
for i in 1 .. paramCount():
|
||||||
|
testsToRun.incl(paramStr(i))
|
||||||
|
|
||||||
# These two procs are added as workarounds for
|
# These two procs are added as workarounds for
|
||||||
# https://github.com/nim-lang/Nim/issues/5549
|
# https://github.com/nim-lang/Nim/issues/5549
|
||||||
proc suiteEnded() =
|
proc suiteEnded() =
|
||||||
|
|
@ -335,7 +356,7 @@ template suite*(name, body) {.dirty.} =
|
||||||
## [Suite] test suite for addition
|
## [Suite] test suite for addition
|
||||||
## [OK] 2 + 2 = 4
|
## [OK] 2 + 2 = 4
|
||||||
## [OK] (2 + -2) != 4
|
## [OK] (2 + -2) != 4
|
||||||
bind formatters, ensureFormattersInitialized, suiteEnded
|
bind formatters, ensureInitialized, suiteEnded
|
||||||
|
|
||||||
block:
|
block:
|
||||||
template setup(setupBody: untyped) {.dirty, used.} =
|
template setup(setupBody: untyped) {.dirty, used.} =
|
||||||
|
|
@ -348,7 +369,7 @@ template suite*(name, body) {.dirty.} =
|
||||||
|
|
||||||
let testSuiteName {.used.} = name
|
let testSuiteName {.used.} = name
|
||||||
|
|
||||||
ensureFormattersInitialized()
|
ensureInitialized()
|
||||||
try:
|
try:
|
||||||
for formatter in formatters:
|
for formatter in formatters:
|
||||||
formatter.suiteStarted(name)
|
formatter.suiteStarted(name)
|
||||||
|
|
@ -370,9 +391,9 @@ template test*(name, body) {.dirty.} =
|
||||||
## .. code-block::
|
## .. code-block::
|
||||||
##
|
##
|
||||||
## [OK] roses are red
|
## [OK] roses are red
|
||||||
bind shouldRun, checkpoints, formatters, ensureFormattersInitialized, testEnded
|
bind shouldRun, checkpoints, formatters, ensureInitialized, testEnded
|
||||||
|
|
||||||
ensureFormattersInitialized()
|
ensureInitialized()
|
||||||
|
|
||||||
if shouldRun(name):
|
if shouldRun(name):
|
||||||
checkpoints = @[]
|
checkpoints = @[]
|
||||||
|
|
@ -433,14 +454,14 @@ template fail* =
|
||||||
## fail()
|
## fail()
|
||||||
##
|
##
|
||||||
## outputs "Checkpoint A" before quitting.
|
## outputs "Checkpoint A" before quitting.
|
||||||
bind ensureFormattersInitialized
|
bind ensureInitialized
|
||||||
|
|
||||||
when declared(testStatusIMPL):
|
when declared(testStatusIMPL):
|
||||||
testStatusIMPL = FAILED
|
testStatusIMPL = FAILED
|
||||||
else:
|
else:
|
||||||
programResult += 1
|
programResult += 1
|
||||||
|
|
||||||
ensureFormattersInitialized()
|
ensureInitialized()
|
||||||
|
|
||||||
# var stackTrace: string = nil
|
# var stackTrace: string = nil
|
||||||
for formatter in formatters:
|
for formatter in formatters:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue