tests: measure test time

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-01-16 23:59:05 +01:00
parent 2ad7c31c85
commit d4672fa057
No known key found for this signature in database
GPG Key ID: 0FE274EE8C95166E

View File

@ -1,3 +1,22 @@
import pkg/asynctest/chronos/unittest
import times, strutils
type
TimedOutputFormatter* = ref object of ConsoleOutputFormatter
testStartTime: float
method testStarted*(formatter: TimedOutputFormatter, testName: string) {.gcsafe.} =
formatter.testStartTime = epochTime()
method testEnded*(formatter: TimedOutputFormatter, testResult: TestResult) =
let time = epochTime() - formatter.testStartTime
let timeStr = time.formatFloat(ffDecimal, precision = 8)
# There doesn't seem to be an easy way to override the echo in the base class
# without changing std/unittest, or copying most of the code here.
# We use a second line as a workaround.
procCall formatter.ConsoleOutputFormatter.testEnded(testResult)
echo " time = ", timeStr, " sec"
addOutputFormatter(TimedOutputFormatter())
export unittest