单元测试期间Ada中的异常处理

时间:2012-10-25 17:14:00

标签: unit-testing exception-handling ada

我正在尝试为我最近编写的一些Ada代码编写一些单元测试,我有一个特殊情况,我希望得到一个异常(如果代码工作正常我不会,但在这种情况下我只是做是测试,而不是编写代码)。如果我在测试例程中处理异常,那么我不知道如何在该过程中继续测试。

即。 (这是一个很好的例子而不是可编译的代码)

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

我的第一个想法是,如果我有一个“更深层的功能”,它实际上是通过该功能进行的,并且通过该功能返回了异常。

即。 (这是一个很好的例子而不是可编译的代码)

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;

理论上我希望这会起作用,我只是讨厌在我的test_function进行实际测试时必须继续编写子函数。

有没有办法在命中异常IN Test_Function后继续执行,而不是必须编写包装函数并通过它调用? 要么 是否有更简单/更好的方法来处理这种情况?

*对于糟糕的代码示例感到抱歉,但我认为这个想法应该是明确的,如果不是,我会重新编写代码。

2 个答案:

答案 0 :(得分:4)

您可以在循环内添加一个块。 使用伪语法,它看起来像:

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

答案 1 :(得分:2)

您可能希望查看the AUnit documentation

中的“Assert_Exception”过程和文档

相关的例子是:

      -- 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;
相关问题