2012-10-25 39 views
3

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

I.E. (这是非常例子而不是编译代码)

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 

我首先想到的是,如果我有一个“更深层次的功能”,这实际上做的通话和异常是通过一个返回。

I.E. (这是非常例子而不是编译代码)

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后继续执行,而不必编写包装函数并通过该函数调用? 或 有没有更容易/更好的方式来处理这种情况?

*对不起,代码不好的例子,但我认为这个想法应该清楚,如果不是,我会重新编写代码。

回答

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 
+0

这几乎是要求的。一般来说,我仍然更喜欢使用子例程,但上面是“本地化异常处理程序”的Ada-ese。 –

+0

我会试一试并回来,我认为(显然不正确),异常是需要在调用函数(过程)的末尾。 – onaclov2000

2

你可能想看看进入“Assert_Exception”程序和文档中the AUnit documentation

相关的例子是:

 -- 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;