Recent

Author Topic: How to Access the TestDecorator  (Read 751 times)

DavidAM

  • Newbie
  • Posts: 4
How to Access the TestDecorator
« on: June 01, 2024, 10:03:32 pm »
How do you use the Test Decorator in fpcunit?

The Wiki (https://wiki.freepascal.org/fpcunit#Test_decorator:_OneTimeSetup_and_OneTimeTearDown) describes how to create the Test Decorator and the benefit of the OneTimeSetup method. However, it does not explain how to access it from within the TestClass.

In this simplified example (which is essentially what the Wiki shows), how would I access the Filename property?

Code: Pascal  [Select][+][-]
  1. type
  2.   { TTestLogSetup }
  3.  
  4.   TTestLogSetup = class(TTestSetup)
  5.     private
  6.       F_Filename: string;
  7.  
  8.     protected
  9.       procedure OneTimeSetup(); override;
  10.       procedure OneTimeTearDown(); override;
  11.  
  12.       property Filename: string read F_Filename;
  13. end;
  14.  
  15. ...
  16.  
  17. implementation
  18.  
  19. {TTestLogSetup}
  20.  
  21. procedure TTestLogSetup.OneTimeSetup();
  22. begin
  23.   F_Filename := './logger.log';
  24. end;
  25.  
  26. procedure TTestLogSetup.OneTimeTearDown();
  27. begin
  28.  
  29. end;
  30.  
  31. ...
  32.  
  33.  
  34.   RegisterTestDecorator(TTestLogSetup, TTestLog);

This is the Test Case:

Code: Pascal  [Select][+][-]
  1.   { TTestLog }
  2.  
  3.   TTestLog = class(TTestCase)
  4.   protected
  5.     //procedure SetUp; override;
  6.     //procedure TearDown; override;
  7.  
  8.   published
  9.     procedure TestInit;
  10. end;
  11.  
  12. implementation
  13.  
  14. ...
  15.  
  16. {TTestLogSetup}
  17.  
  18. procedure TTestLog.TestInit;
  19. var
  20.   sMsg: string;
  21.   oLog: TMadLogger;
  22. begin
  23.   oLog := TMadLogger.Create( { I need the Filename here }, mllDevelopment);
  24.   try
  25.     sMsg := 'Start Logger';
  26.     oLog.LogInitMsg(sMsg);
  27.  
  28.     oLog.Filepath := { I need the Filename here, too };         // Closes the file
  29.     oLog.Append := True;
  30.  
  31.     sMsg := 'Continue Logging';   // Re-opens the file
  32.     oLog.LogInfoMsg(sMsg);
  33.   finally
  34.     oLog.Free();
  35.   end;
  36. end;

I am trying to learn using this simplified example. Ultimately, I might want to create the Logger object once (in the Decorator) and use it throughout the multiple tests that will be written.

I have Googled this extensively, and I cannot find a definitive answer. I did find a post on Stack Exchange that suggested using class methods/properties but that answer was referring to DUnit and raised some controversy. I am searching for the intention of how this is supposed to work.

 

TinyPortal © 2005-2018