onaclov2000
onaclov2000

Reputation: 5841

Exception Handling in Ada during Unit Test

I am attempting to write some unit tests for some Ada code I recently wrote, I have a particular case where I am expecting to get an Exception (if the code worked correctly I wouldn't but in this case all I'm doing is testing, not writing code). If I handle the exception in the Testing routine then I don't see how I can continue testing in that procedure.

I.E. (This is very much an example and NOT compilable code)

procedure Test_Function is
begin
  from -20 to 20
     Result := SQRT(i);

 if Result = (Expected) then
     print "Passed";
 end_if;

exception:
  print "FAILED";
end Test_Function

My first thought is if I had a "deeper function" which actually did the call and the exception returned through that.

I.E. (This is very much an example and NOT compilable code)

procedure Test_Function is
begin
  from -20 to 20
     Result := my_SQRT(i);

 if Result = (Expected) then
     print "Passed";
 end_if;

exception:
  print "FAILED";
end Test_Function

function my_SQRT(integer) return Integer is
begin
   return SQRT(i);
exception:
   return -1;
end my_SQRT;

And in theory I expect that would work, I just hate to have to keep writing sub functions when my test_function, is expected to do the actual testing.

Is there a way to continue execution after hitting the exception IN Test_Function, rather than having to write a wrapper function and calling through that? OR Is there a easier/better way to handle this kind of scenario?

*Sorry for the poor code example, but I think the idea should be clear, if not I'll re-write the code.

Upvotes: 3

Views: 875

Answers (2)

R. Tyler Croy
R. Tyler Croy

Reputation: 101

You might want to look into the "Assert_Exception" procedure and documentation in the AUnit documentation

The relevant example being:

      -- Declared at library level:
         procedure Test_Raising_Exception is
         begin
            call_to_the_tested_method (some_args);
         end Test_Raising_Exception;

      -- In test routine:
      procedure My_Routine (...) is
      begin
         Assert_Exception (Test_Raising_Exception'Access, String_Description);
      end;

Upvotes: 2

egilhh
egilhh

Reputation: 6430

You can add a block inside the loop. Using your pseudo-syntax, it will look something like:

procedure Test_Function is
begin
  from -20 to 20
    begin
      Result := SQRT(i);

      if Result = (Expected) then
         print "Passed";
      end_if;

    exception:
      print "FAILED";
    end;
  end loop;
end Test_Function

Upvotes: 3

Related Questions