Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-11

Testing if exception raised in fpcUnit

8 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

ik

Hello,

I wish to test if a method has raised an exception for a value (something that should make the test pass) or not.

For example:

function TestNum(ch : Char) : Byte;
begin
  if UpCase(ch) in ['A'..'F', '0'..'9'] then
    ...
  else
    raise Exception.Create('Invalid char was given.');
end;

I want to test the code with Char like 'y' for example, to make sure that it does raises the exception.

Does AssertException is the proper method for such test ?

Thanks,
Ido

Michael Van Canneyt
to ik
ik wrote 20 lines
Hello, I wish to test if a method has raised an exception for a value (something that should make the test pass) or not. For example: function TestNum(ch : Char) : Byte; begin if UpCase(ch) in ['A'..'F', '0'..'9'] then ... else raise Exception.Create('Invalid char was given.'); end; I want to test the code with Char like 'y' for example, to make sure that it does raises the exception. Does AssertException is the proper method for such test ?

Yes.

Michael.

ik
to Michael Van Canneyt
Michael Van Canneyt wrote 27 lines
On Mon, 5 Nov 2012, ik wrote: > Hello, > > I wish to test if a method has raised an exception for a value > (something that should make the test pass) or not. > > For example: > > function TestNum(ch : Char) : Byte; > begin > if UpCase(ch) in ['A'..'F', '0'..'9'] then > ... > else > raise Exception.Create('Invalid char was given.'); > end; > > I want to test the code with Char like 'y' for example, to make sure > that it does raises the exception. > > > Does AssertException is the proper method for such test ? Yes.

So how do I do it ?

TRunMethod = procedure of object;

If I pass it a function, it don't really like it:

AssertException(ExceptionWasNotRaisd, EMsgPackWrongType, @MsgPackType.AsByte);

convert_testcase.pas(592,60) Error: Incompatible type for arg no. 3: Got "<procedure variable type of function:Byte of object;Register>", expected "<procedure variable type of procedure of object;Register>"

Show quoted text 5 lines
Michael. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Michael Van Canneyt
to ik
ik wrote 37 lines
On Mon, Nov 5, 2012 at 2:45 PM, Michael Van Canneyt wrote: > > > On Mon, 5 Nov 2012, ik wrote: > >> Hello, >> >> I wish to test if a method has raised an exception for a value >> (something that should make the test pass) or not. >> >> For example: >> >> function TestNum(ch : Char) : Byte; >> begin >> if UpCase(ch) in ['A'..'F', '0'..'9'] then >> ... >> else >> raise Exception.Create('Invalid char was given.'); >> end; >> >> I want to test the code with Char like 'y' for example, to make sure >> that it does raises the exception. >> >> >> Does AssertException is the proper method for such test ? > > > Yes. So how do I do it ? TRunMethod = procedure of object; If I pass it a function, it don't really like it: AssertException(ExceptionWasNotRaisd, EMsgPackWrongType, @MsgPackType.AsByte);

You must create a method in your testcase that calls the funtion, and pass that method to AssertException.

The alternative is to do everything yourself:

Procedure TMyTestClass.MyTest;
Var
   B : Byte;
begin
   try
     B:=MsgPackType.AsByte;
     Fail(ExceptionWasNotRaisd);
   except
     On E : Exception do
      if (E is not EMsgPackWrongType) then
        Fail(ExceptionWasNotRaisd);
   end;
end;

Michael.

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 17 lines
On 2012-11-05 12:49, ik wrote: > > So how do I do it ? Create a method of the test class, and pass that to AssertException. Alternatively, do it manually as follows: try si.SetSlideName('001~z~1~a~4~d~1.1.2.swf'); si.SlideTypeDB; except on E: exception do begin CheckEquals(EUnknownSlideType.ClassName, E.ClassName, 'Failing on 2'); end; end;

This will not detect if no exception is raised, only if the right exception is raised in case one is raised.

Michael.

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
This will not detect if no exception is raised, only if the right exception is raised in case one is raised.

You are correct. An extra test is needed just before the except block. As your example shows, simply calling Fail(...) if no exception was raised.

Regards,
- Graeme -

ik
to Graeme Geldenhuys
Graeme Geldenhuys wrote 8 lines
On 2012-11-05 13:07, Michael Van Canneyt wrote: > > This will not detect if no exception is raised, only if the right > exception is raised in case one is raised. You are correct. An extra test is needed just before the except block. As your example shows, simply calling Fail(...) if no exception was raised.

That's what I'ved done

Thanks :)

Show quoted text 8 lines
Regards, - Graeme - _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal