Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/sampletests.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ../unittest2

suite "Sample Tests":
test "Sample Test":
check(1 == 1)

test "Global test":
check(1 == 1)

suite "Sample Suite":
test "Sample Test":
check(1 == 1)

test "Sample Test 2":
check(1 == 1)

test "Sample Test 3":
check(1 == 1)

test "another global test":
check(1 == 1)

for i in 0..<10:
test "test" & $i:
echo "hello"
12 changes: 12 additions & 0 deletions tests/tunittest.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ discard """

import ../unittest2, sequtils
from std/exitprocs import nil
import std/[osproc, strutils]

#------------------------------------------------------------------------------
# Tests using backdoors
Expand Down Expand Up @@ -256,3 +257,14 @@ suite "break should works inside test body":
number = 3
test "step three":
check number == 2

suite "list tests":
test "should list tests when `-d:unittest2ListTests` is passed":

let (output, exitCode) = execCmdEx("nim c -d:unittest2ListTests -r tests/sampletests.nim")

check exitCode == 0
check count(output, "Suite:") == 3
check count(output, "Test:") == 16
check count(output, "File:") == 16
check count(output, "Hello") == 0
66 changes: 45 additions & 21 deletions unittest2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ const
## enabled at compile-time meaning that tests must be written
## conservatively. `suite` features (`setup` etc) in particular are not
## supported.
unittest2ListTests* {.booldefine.} = false
## List tests at runtime (useful for test runners)

when useTerminal:
import std/terminal

const
collect = (not unittest2NoCollect and not unittest2Compat) or unittest2PreviewIsolate
collect = (not unittest2NoCollect and not unittest2Compat) or unittest2PreviewIsolate or unittest2ListTests
autoParseArgs = not unittest2DisableParamFiltering
isolate = unittest2PreviewIsolate

Expand All @@ -187,6 +189,8 @@ type
suiteName: string
testName: string
impl: proc(suite, name: string): TestStatus
lineInfo: int
filename: string

TestStatus* = enum ## The status of a test when it is done.
OK,
Expand Down Expand Up @@ -1137,7 +1141,8 @@ template runtimeTest*(nameParam: string, body: untyped) =
defer: failingOnExceptions("[teardown] "):
when declared(testTeardownIMPLFlag): testTeardownIMPL()
failingOnExceptions(""):
body
when not unittest2ListTests:
body

checkpoints = @[]

Expand All @@ -1152,7 +1157,13 @@ template runtimeTest*(nameParam: string, body: untyped) =
if shouldRun(localSuiteName, localTestName):
let
instance =
Test(testName: localTestName, suiteName: localSuiteName, impl: runTest)
Test(
testName: localTestName,
suiteName: localSuiteName,
impl: runTest,
lineInfo: instantiationInfo().line,
filename: instantiationInfo().filename
)
when collect:
tests.mgetOrPut(localSuiteName, default(seq[Test])).add(instance)
else:
Expand All @@ -1171,9 +1182,11 @@ template dualTest*(nameParam: string, body: untyped) =
## Similar to `test` but run the test both compuletime and run time, no
## matter the `unittest2Static` flag
staticTest nameParam:
body
when not unittest2ListTests:
body
runtimeTest nameParam:
body
when not unittest2ListTests:
body

template test*(nameParam: string, body: untyped) =
## Define a single test case identified by `name`.
Expand All @@ -1192,9 +1205,11 @@ template test*(nameParam: string, body: untyped) =
when nimvm:
when unittest2Static:
staticTest nameParam:
body
when not unittest2ListTests:
body
runtimeTest nameParam:
body
when not unittest2ListTests:
body

{.pop.} # raises: []

Expand Down Expand Up @@ -1470,24 +1485,33 @@ when collect:
# supported
while tests.len > 0:
var tmp = move(tests)
suiteRunStarted(tmp)
for suiteName, suite in tmp:
if suite.len == 0: continue

suiteStarted(suiteName)
for test in suite:
when isolate:
if not isolated:
runIsolated(test)
when unittest2ListTests:
for suiteName, suite in tmp:
if suite.len == 0: continue
echo "Suite: ", suiteName
for test in suite:
echo "\tTest: ", test.testName
echo "\tFile: ", test.filename, ":", test.lineInfo
else:
suiteRunStarted(tmp)
for suiteName, suite in tmp:
if suite.len == 0: continue

suiteStarted(suiteName)
for test in suite:
when isolate:
if not isolated:
runIsolated(test)
else:
runDirect(test)
else:
runDirect(test)
else:
runDirect(test)

suiteEnded()
suiteEnded()

suiteRunEnded()
testRunEnded()
suiteRunEnded()
when not unittest2ListTests:
testRunEnded()

addExitProc(runScheduledTests)

Expand Down