2012-10-25 52 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;