From d4672fa057fcbd01475512c4fd7b3f9f142c0959 Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Thu, 16 Jan 2025 23:59:05 +0100 Subject: [PATCH] tests: measure test time Signed-off-by: Csaba Kiraly --- tests/asynctest.nim | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/asynctest.nim b/tests/asynctest.nim index 7c6a4afd..01c38716 100644 --- a/tests/asynctest.nim +++ b/tests/asynctest.nim @@ -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