[英]Correct way to temporarily enable and disable function wrapping in cmocka?
I am using the cmocka library to test some embedded c code. 我正在使用cmocka库来测试一些嵌入式c代码。 According to the documentation I use the
__wrap_
prefix to mock functions so I can isolate my unit tests. 根据文档我使用
__wrap_
前缀来模拟函数,所以我可以隔离我的单元测试。 However once I do that all calls to the function forever go to the wrapped function. 但是,一旦我这样做,所有对函数的调用将永远转到包装函数。 How do I re-enable the real function in certain circumstances so I can test it or allow other functions to use it?
如何在某些情况下重新启用实际功能,以便我可以测试它或允许其他功能使用它? It seems to me the only way is to use a global field as a switch to call the real function like so:
在我看来,唯一的方法是使用全局字段作为开关来调用真实函数,如下所示:
int __wrap_my_function(void) {
if (g_disable_wrap_my_function) {
return __real_my_function();
}
// ... do mock stuff
}
Is that the correct way to go about it? 这是正确的方法吗?
You simply compile without the -wrap command line option. 您只需在没有-wrap命令行选项的情况下进行编译。
Or you use defines: 或者您使用定义:
#include <cmocka.h>
#ifdef UNIT_TESTING
#define strdup test_strdup
#endif
Add the mock function test_strdup. 添加模拟函数test_strdup。 You can now use this function to test.
您现在可以使用此功能进行测试。
I ended up doing exactly what I suggested in my question. 我最终完成了我在问题中提出的建议。 I used a global variable that I check in the wrapped function to eanble and disable the mocking at runtime.
我使用了一个全局变量,我检查了包装函数,以便在运行时调用和禁用模拟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.