Exception Handling in Ada during Unit Test

空扰寡人 提交于 2019-12-12 13:08:59

问题


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.


回答1:


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



回答2:


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;


来源:https://stackoverflow.com/questions/13073793/exception-handling-in-ada-during-unit-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!