有没有办法在 Ada 单元测试中测试抛出异常?

Is there a way to test for the throwing of an exception in a Ada unit test?

我创建了一些单元测试,其中一个需要测试在发生一系列非法活动后是否抛出异常。这是一组实用程序代码,因此预计 invalid/illegal 值将在将来的某个时间由某些程序员输入。

我看到 this question 使用 Ada 单元测试框架 - 在目前的情况下,这不是一个选项;但是,我可能会重新处理它们以便能够使用它(我的理解是将单元测试项目添加到现有的遗留代码库是一个耗时的过程)。

那么-我应该在这个函数的主体中填写什么,以便在遇到多个异常时它只 returns 为真?

--* Returns true if an exception is yielded for test cases 5 and 6
function VerifyInvalidValuesCauseExceptions return Boolean is
begin

  --Run tests for 5 and 6, assert that exception is thrown for each
  --Not exactly sure how to do this yet

  return false;
end;
Raising_An_Exception :
begin
   Should_Raise_A_Constraint_Error;

   Ahven.Fail (Message => "Exception not raised as expected.");
exception
   when Constraint_Error =>
      null;
end Raising_An_Exception;

所以-我最终做的是 Jacob 的回答和一些评论的组合。事实证明异常 begin .. end 可以嵌套(哎呀!)。

--  Returns true if an exception is yielded for test cases 5 and 6
function Verify_Invalid_Values_Cause_Exceptions return Boolean is
begin

   Case_5 :
   declare
      Test_Output : Data_Type;
   begin
      --  Run conversions, assert that exception is thrown for each
      Test_Output := Function_That_Throws_Exception (Test_Case_5);

      return False; --  Should not get here
   exception 
      when Constraint_Error =>
         null; --  Should get here
   end Case_5;

   Case_6 :
   declare
      Test_Output : Data_Type;
   begin
      --  Run conversions, assert that exception is thrown for each
      Test_Output := Function_That_Throws_Exception (Test_Case_6);

      return False; --  Should not get here
   exception 
      when Constraint_Error =>
         null; --  Should get here
   end Case_6;

   return True;
end Verify_Invalid_Values_Cause_Exceptions;

这是一个在一个验证函数中包含多个测试用例的工作演示。我可能更愿意为每个测试提供自己的测试程序。

想法是测试用例 5 和 6 应该引发 Constraint_Error,而其他的则不会。

with Exception_Capable_Test;
with Ada.Text_IO;
procedure Verify_Exception_Tests is
   function Verify return Boolean is
   begin
      begin
         Exception_Capable_Test (4);
      exception
         when Constraint_Error =>
            return False;
      end;
      begin
         Exception_Capable_Test (5);
         return False;
      exception
         when Constraint_Error =>
            null;
      end;
      begin
         Exception_Capable_Test (6);
         return False;
      exception
         when Constraint_Error =>
            null;
      end;
      begin
         Exception_Capable_Test (7);
      exception
         when Constraint_Error =>
            return False;
      end;
      return True;
   end Verify;
begin
   Ada.Text_IO.Put_Line
     ("Verify " & (if Verify then "passed" else "failed"));
end Verify_Exception_Tests;

被测程序规格:

procedure Exception_Capable_Test (Test_Case : Positive);

及其正文:

procedure Exception_Capable_Test (Test_Case : Positive) is
begin
   case Test_Case is
      when 5 | 6 =>
         raise Constraint_Error with "failed with case" & Test_Case’Img;
      when others =>
         null;
   end case;
end Exception_Capable_Test;

这适用于(打印 Verify passed)GCC 6.1.0、7.1.0 和 GNAT GPL 2016、7。