单元测试期间Ada中的异常处理 [英] Exception Handling in Ada during Unit Test

查看:83
本文介绍了单元测试期间Ada中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我最近编写的某些Ada代码编写一些单元测试,在一个特殊的情况下,我希望得到一个异常(如果代码正常工作,我不会,但是在这种情况下,我会就是测试,而不是编写代码。如果我在测试例程中处理了异常,则看不到如何继续执行该过程。

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.

IE (这是一个示例,并且不是可编译的代码)

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.

IE (这是一个示例,并且不是可编译的代码)

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.

是否有办法在遇到异常IN Test_Function之后继续执行,而不必编写包装函数并通过调用那?

是否有更简单/更好的方式来处理这种情况?

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.

推荐答案

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

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

这篇关于单元测试期间Ada中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆