Recent

Author Topic: Exceptions en FPCUnit  (Read 369 times)

janasoft

  • New Member
  • *
  • Posts: 42
Exceptions en FPCUnit
« on: June 20, 2025, 11:59:32 pm »
I'm starting to use FPCUnit and I don't know how to test the appearance of an exception. I understand that ExpectException or AssertException should be used but I don't know how to do it.
Lazarus 2.2.6 (rev lazarus_2_2_6) FPC 3.2.2 x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2438
Re: Exceptions en FPCUnit
« Reply #1 on: June 21, 2025, 02:52:22 am »
I'm starting to use FPCUnit and I don't know how to test the appearance of an exception. I understand that ExpectException or AssertException should be used but I don't know how to do it.
In the AssertException, you explicitly specify the method for performing testing and the class of exception that should occur in it.
You call an ExpectException before running a test, usually when developing a test class, for the same purpose.

janasoft

  • New Member
  • *
  • Posts: 42
Re: Exceptions en FPCUnit
« Reply #2 on: June 21, 2025, 10:06:39 am »
Is this the correct way to use AssertException?:

unit under test
Code: Pascal  [Select][+][-]
  1. function TLogAnalyzer.IsValidLogFileName(Filename: string): Boolean;
  2. begin
  3.     if Filename = '' then
  4.       raise EFileNameEmptyException.Create('No filename provided!')
  5. end;


Test
Code: Pascal  [Select][+][-]
  1.   { TTestCase1 }
  2.  
  3.   TTestCase1= class(TTestCase)
  4.   private
  5.     analyzer: TLogAnalyzer;
  6.   protected
  7.     procedure ExceptionHelper;
  8.   published
  9.     procedure IsValidFileName_EmptyFileName_Exception;
  10.   end;
  11.  
  12. implementation
  13.  
  14. procedure TTestCase1.ExceptionHelper;
  15. begin
  16.   analyzer.IsValidLogFileName('');
  17. end;
  18.  
  19. procedure TTestCase1.IsValidFileName_EmptyFileName_Exception;
  20. begin
  21.   try
  22.     analyzer:= TLogAnalyzer.Create;
  23.     AssertException(EFileNameEmptyException, @ExceptionHelper, 'No filename provided!');
  24.   finally
  25.     analyzer.Free;
  26.   end;
  27. end;
You call an ExpectException before running a test, usually when developing a test class, for the same purpose.
Could you give some example of use?
Regards
Lazarus 2.2.6 (rev lazarus_2_2_6) FPC 3.2.2 x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2438
Re: Exceptions en FPCUnit
« Reply #3 on: June 21, 2025, 12:54:33 pm »
Could you give some example of use?
Selftests in ~/lazarus/fpc/3.2.2/source/packages/fcl-fpcunit/src/tests/

 

TinyPortal © 2005-2018