Commits

Shinya Okano committed d58088c

Changed: measure elapsed time

Comments (0)

Files changed (2)

Example/MyUnit1Test.pas

 
 uses
   Nullpobug.UnitTest
+  , System.SysUtils
   , MyUnit1
   ;
 
     procedure TestSub;
     procedure TestAddFail;
     procedure TestSkip;
+    procedure TestSlow;
   end;
 
 implementation
   raise ESkipTest.Create('This is Skipped.');
 end;
 
+procedure TMyUnit1Test.TestSlow;
+begin
+  Sleep(500);
+end;
+
 initialization
   RegisterTest(TMyUnit1Test);
 

Nullpobug.UnitTest.pas

     FError: Exception;
     FTestMethodName: String;
     FTestCaseName: String;
+    FTime: Int64;
   public
     constructor Create;
     destructor Destroy; override;
     property Error: Exception read FError write FError;
     property TestMethodName: String read FTestMethodName write FTestMethodName;
     property TestCaseName: String read FTestCaseName write FTestCaseName;
+    property Time: Int64 read FTime write FTime;
   end;
 
   TOnRanTestMethod = procedure(TestResult: TTestResult) of object;
   private
     FTestSuiteList: TObjectList<TTestSuite>;
     FTestResultList: TObjectList<TTestResult>;
+    FStopWatch: TStopWatch;
   public
     constructor Create;
     destructor Destroy; override;
     procedure Run(TestSuite: TTestSuite); virtual;
     procedure RunTests; virtual;
     property TestResultList: TObjectList<TTestResult> read FTestResultList;
+    property StopWatch: TStopWatch read FStopWatch;
   end;
 
   TTextTestRunner = class(TTestRunner)
 begin
   FResultType := rtSkip;
   FError := nil;
+  FTime := 0;
 end;
 
 destructor TTestResult.Destroy;
   RttiType: TRttiType;
   Method: TRttiMethod;
   TestResult: TTestResult;
+  StopWatch: TStopWatch;
 begin
+  StopWatch := TStopWatch.Create;
+  StopWatch.Start;
   RttiContext := TRttiContext.Create;
   try
     RttiType := RttiContext.GetType(ClassType);
         TestResult.ResultType := rtOk;
         TestResult.TestMethodName := Method.Name;
         TestResult.TestCaseName := ToString;
+        StopWatch.Reset;
         try
           Method.Invoke(Self, []);
         except
             TestResult.Error := E;
           end;
         end;
+        TestResult.Time := StopWatch.ElapsedMilliseconds;
         if Assigned(FOnRanTestMethod) then
           FOnRanTestMethod(TestResult);
         TestResultList.Add(TestResult);
     end;
   finally
     RttiContext.Free;
+    FreeAndNil(StopWatch);
   end;
 end;
 
 begin
   FTestSuiteList := TObjectList<TTestSuite>.Create;
   FTestResultList := TObjectList<TTestResult>.Create;
+  FStopWatch := TStopWatch.Create;
+  FStopWatch.Start;
 end;
 
 destructor TTestRunner.Destroy;
 begin
+  FreeAndNil(FStopWatch);
   FreeAndNil(FTestSuiteList);
   FreeAndNil(FTestResultList);
   inherited Destroy;
 end;
 
 procedure TTextTestRunner.WriteFooter;
+var
+  Seconds: Single;
 begin
+  Seconds := StopWatch.ElapsedMilliseconds / 1000;
   WriteLn('');
   WriteLn(DupeString('-', 70));
-  WriteLn(Format('Ran %d tests in 0.000s', [TestResultList.Count]));
+  WriteLn(Format('Ran %d tests in %.3fs', [TestResultList.Count, Seconds]));
   WriteLn('');
   WriteLn('OK');
 end;