Commits

Shinya Okano  committed d089deb

impl: SetUp, TearDown method

  • Participants
  • Parent commits d58088c

Comments (0)

Files changed (3)

File Example/MyUnit2.pas

 
 interface
 
+type
+  TPerson = class(TObject)
+  private
+    FName: String;
+  public
+    constructor Create(Name: String);
+    function GetName: String;
+  end;
+
 implementation
 
+constructor TPerson.Create(Name: String);
+begin
+  FName := Name;
+end;
+
+function TPerson.GetName: String;
+begin
+  Result := FName;
+end;
+
 end.

File Example/MyUnit2Test.pas

 interface
 
 uses
-  MyUnit2
+  Nullpobug.UnitTest
+  , MyUnit2
   ;
 
+type
+  TMyUnit2Test = class(TTestCase)
+  private
+    FPerson: TPerson;
+  published
+    procedure SetUp; override;
+    procedure TearDown; override;
+    procedure TestGetName;
+  end;
+
 implementation
 
+procedure TMyUnit2Test.SetUp;
+begin
+  FPerson := TPerson.Create('Foo');
+end;
+
+procedure TMyUnit2Test.TearDown;
+begin
+  FPerson.Free;
+end;
+
+procedure TMyUnit2Test.TestGetName;
+begin
+  AssertEquals(FPerson.GetName, 'Foo');
+end;
+
+initialization
+  RegisterTest(TMyUnit2Test);
+
 end.

File Nullpobug.UnitTest.pas

         TestResult.TestCaseName := ToString;
         StopWatch.Reset;
         try
-          Method.Invoke(Self, []);
-        except
-          on E: EAssertionError do
-          begin
-            TestResult.ResultType := rtFail;
-            TestResult.Error := E;
+          try
+            SetUp;
+            Method.Invoke(Self, []);
+          except
+            on E: EAssertionError do
+            begin
+              TestResult.ResultType := rtFail;
+              TestResult.Error := E;
+            end;
+            on E: ESkipTest do
+            begin
+              TestResult.ResultType := rtSkip;
+              TestResult.Error := E;
+            end;
+            on E: Exception do
+            begin
+              TestResult.ResultType := rtError;
+              TestResult.Error := E;
+            end;
           end;
-          on E: ESkipTest do
-          begin
-            TestResult.ResultType := rtSkip;
-            TestResult.Error := E;
-          end;
-          on E: Exception do
-          begin
-            TestResult.ResultType := rtError;
-            TestResult.Error := E;
-          end;
+        finally
+          TearDown;
         end;
         TestResult.Time := StopWatch.ElapsedMilliseconds;
         if Assigned(FOnRanTestMethod) then