C 单元测试 - 从存根优雅退出例程中返回 [英] C Unit Testing - Returning from a stubbed graceful exit routine

查看:26
本文介绍了C 单元测试 - 从存根优雅退出例程中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的场景.我正在测试的函数有一个错误条件,如果命中,则调用正常退出函数以释放所有全局内存、关闭句柄并退出程序.

Here is the scenario I've got. The function I'm testing has an error condition that, if hit, calls a graceful exit function to free any global memory, close handles, and exit the program.

显然,我想编写一个测试来测试这种情况,以确保它得到正确处理,但我不希望正常退出例程实际退出程序,因为这会停止任何剩余的测试.这意味着存根优雅退出例程.存根和不调用 exit 的问题是控制流返回到被测函数(这很糟糕,因为例程应该退出).

Obviously, I'll want to write a test that tickles this condition to make sure that it is handled correctly but I don't want the graceful exit routine to actually exit the program since that would stop any remaining tests. This means stubbing the graceful exit routine. The problem with stubbing and not calling exit is that the flow of control returns to the function under test (which is bad since the routine was supposed to exit).

这里是实际的问题:我们如何将控制权从存根函数返回给测试而不是被测函数?

Here is the actual question: How do we return control from the stubbed function to the test instead of to the function under test?

我可以执行 setjmp/longjmp,但由于goto"通常很糟糕,我希望有任何其他建议.(请记住,这是过程 C,而不是 C++,因此据我所知,异常不会起作用)

I can do a setjmp / longjmp, but since "gotos" are bad in general, I'd love any other suggestions. (Keep in mind that this is procedural C, not C++, so exceptions aren't going to work as far as I know)

编辑正如 Soren 和其他人在下面所建议的,在测试时让 exit 什么都不做是一个好主意.有几种方法可以做到这一点,无论是通过 #define 语句还是 exit() 例程的存根.

EDIT As Soren and others have suggested below, making exit do nothing when testing is a great idea. There are several ways to do this whether it be through a #define statement or a stub for the exit() routine.

然而,这样做会带来我真正想要解决的问题(setjmp/longjmp 除外).看看这个场景:

HOWEVER, doing so presents the problem that I'm really after a solution for (other than setjmp / longjmp). Take a look at this scenario:

void gracefulExit() {
    // Clean Up
    exit();
}

void routineUnderTest() {
    // Do some calcs

    if (someBadCondition == TRUE)
        gracefulExit()

    // Do some more calcs
}

如果 exit() 在这种情况下什么都不做,gracefulExit() 会将控制权返回给被测例程,这不应该发生.因此,我需要一种方法让 exit()(或优雅退出()的存根版本)将控制权返回给测试而不是被测函数.

If exit() does nothing in this scenario, gracefulExit() will return control back to the routine under test, which should not happen. Hence, I need a way to make exit() (or the stubbed version of gracefulExit()) return control to the test instead of the function under test.

setjmp/longjmp (aka goto) 是一种方法,虽然不是真正优雅的方法.关于如何解决这个问题的任何想法?

setjmp / longjmp (aka goto) is a way to do this, although not really an elegant way. Any ideas on how to solve that?

编辑 #2

正如 fizzer 提到的,setjmp/longjmp 是处理这种情况的有效方法.这是我永久处理它的可能方式.

As fizzer mentioned, setjmp/longjmp is a valid way to handle this situation. It is the likely way that I will handle it permanently.

但是,我从一位同事那里收到了另一种可能的解决方案.不要将gracefulExit() 例程#定义为存根例程,而是执行以下操作:

I have, however, received another possible solution from a co-worker. Instead of #defining the gracefulExit() routine to a stub routine, do the following:

#define gracefulExit return NULL

被测试的特定函数可以很好地处理这个问题,因为 NULL 是它的有效返回值.我还没有在所有可能的情况下对此进行测试(例如,具有 void 返回值的函数).如前所述,我可能会使用 setjmp/longjmp 方法来解决这个问题,但如果这个额外的解决方案能激发一些人的灵感,那就太好了!

The particular function under test handles this just fine since NULL is a valid return value for it. I haven't tested this in every possible case yet (e.g. a function that has a void return value). As mentioned earlier, I will likely use the setjmp/longjmp way of solving this problem, but if this additional solution sparks an idea for somebody, great!

推荐答案

我使用 setjmp/longjmp(封装在一个方便的宏中)来处理这个和类似的场景(例如测试断言失败).

I use setjmp / longjmp (wrapped in a convenience macro) for this and similar scenarios (e.g. test that an assertion failed).

这篇关于C 单元测试 - 从存根优雅退出例程中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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