- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个项目 using the cmocka framework 。 cmocka主页指出
Test fixtures are setup and teardown functions that can be shared across multiple test cases to provide common functions that prepare the test environment and destroy it afterwards.
但是,docs 都没有I've read解释夹具系统的工作原理。
如果我使用如下代码运行测试
int main(void) {
const struct CMUnitTest license_tests[] = {
cmocka_unit_test(test_setup),
cmocka_unit_test(test_null_app),
cmocka_unit_test(test_null_name),
};
return cmocka_run_group_tests(license_tests, NULL, NULL);
}
我如何/在哪里可以指示 cmocka 运行安装/拆卸固定装置,以及 cmocka 有哪些功能(如果有)可以让我访问在所述固定装置中创建的内容?
最佳答案
这是一个模板单元测试文件,您可以在项目中重复使用。它满足您的所有要求
#include <stdio.h>
#include <cmocka.h>
#include "mylib.h"
// Include here all your mocked functions, see below
#include "testmocks.c"
typedef struct {
mylibobj_t* mylibobj;
} teststate_t;
/**
* This is run once before all group tests
*/
static int groupSetup (void** state) {
teststate_t* teststate = calloc(1, sizeof(teststate_t));
*state = teststate;
return 0;
}
/**
* This is run once after all group tests
*/
static int groupTeardown (void** state) {
teststate_t* teststate = *state;
free(teststate);
return 0;
}
/**
* This is run once before one given test
*/
static int testSetup (void** state) {
teststate_t* teststate = *state;
//Maybe instantiate mylibobj?
return 0;
}
/**
* This is run once after one given test
*/
static int testTeardown (void** state) {
return 0;
}
/**
* This test will success with these options
*/
void mylib_setTime_s0 (void** state) {
teststate_t* teststate = *state;
// Do your testing stuff
}
/**
* This test will fail with these options
*/
void mylib_setTime_f0 (void** state) {
teststate_t* teststate = *state;
// Do your testing stuff
}
int main (void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(mylib_setTime_s0, testSetup, testTeardown),
cmocka_unit_test_setup_teardown(mylib_setTime_f0, testSetup, testTeardown),
};
return cmocka_run_group_tests(tests, groupSetup, groupTeardown);
}
testmocks.c 的存在只是为了代码组织,并且可以保存 0 到 N 对模拟函数
#define MOCKBYPASS -7337
mylib_status_t __real_inner_function (char* id);
mylib_status_t __wrap_inner_function (char* id) {
int mock = mock();
if(mock == MOCKBYPASS)
return __real_inner_function(id);
else
return mock;
}
...
请记住,gcc 编译技巧是这些模拟正常工作的必要条件
关于在 cmocka 中创建夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47232166/
我在文档和服务员示例中进行了搜索,但找不到有关如何检查正确的函数指针是否作为参数传递给函数的示例。 这个示例代码应该详细说明我的意思: void func_A(); void func_B(); vo
我已经下载了 cmocka example files并按照所有说明进行操作。所有测试文件都已成功生成,我可以运行它们,但控制台中没有输出。我曾尝试更改 CMOCKA_MESSAGE_OUTPUT 环
假设我声明了一个名为 foo 的 C 结构,它有一个名为 bar 的 int 字段和一个名为 baz 的 char *。 我如何使用 Cmocka expect_ 和 check_expected 宏
我正在开发一个项目 using the cmocka framework 。 cmocka主页指出 Test fixtures are setup and teardown functions tha
我有一个包含两个数据成员的简单结构。 typedef struct { int32_t x; int32_t y; } MyStructType; 我有一个按值返回结构的函数。 MyS
我正在使用 cmocka用于测试一些嵌入式 C 代码的库。根据文档,我使用 __wrap_ 前缀来模拟函数,这样我就可以隔离我的单元测试。但是,一旦我这样做,所有对该函数的调用将永远转到包装函数。我如
一点背景知识:我希望对构建到共享对象文件中的代码进行单元测试,例如libabc。此代码还调用 libjson-c 和 libcurl 函数。 我计划为产品代码中的每个函数编写函数测试,模拟从 liba
我正在尝试使用 cmocka 对一些源代码进行单元测试。基本上(相关的)源代码看起来类似于 Source.c。 单元测试分别调用每个函数。在测试 Add() 函数时,此函数最终将调用 util_mal
我正在尝试使用 cmocka 库在 c 中编写一个测试用例。我的测试用例正在测试一个函数,然后在内部调用来自第 3 方库的函数(无法修改库)。当应用程序不是时,此函数返回 NULL 值启动并运行,所以
我在 Ubuntu 下使用 Eclipse。 我刚刚安装了 cmocka: Install the project... -- Install configuration: "Debug" -- In
我刚刚开始使用 Cmocka,我已经在这个问题上停留了一段时间。我有一个正在尝试构建的 cmocka 项目。当我尝试使用“make”时,我看到了错误。 [ ERROR ] --- No entries
我有一个小型静态库项目,我正在将其从使用 Makefile 构建改写为现代 CMake,我正在尝试学习它。我的项目大量使用断言来检查先决条件,因此我编写了一个非常简单的自定义断言宏,它有条件地扩展为一
我正在学习 cmocka 单元测试框架。我对 mock() 函数的使用有点困惑。有人可以用合适的例子清楚地解释为什么以及如何使用它吗?还请说明何时应该使用它。 最佳答案 你应该阅读 https://l
我最近遇到了'cmocka ' C 的模拟库。我能够模拟单个函数并成功测试调用函数。现在我需要它来将它用于我正在从事的项目,在该项目中需要模拟大量功能。如何将我想要模拟的所有函数传递给 Linker
我能够使用 cmocka 并在屏幕上获得默认结果。我想获得 JUnit 格式的单元测试结果。 CMocka 通过使用环境变量 CMOCKA_MESSAGE_OUTPUT 或使用 API cmocka_
我很难使用 cmocka、测试 malloc 的失败(模拟)和使用 gcov 来找到一个小问题的答案 关于 cmocka+gcov 的更新:我注意到在我的 cmocka 测试中模拟一个函数后,我得到了
我安装了 CMocka testing framework并尝试了 sample code : #include #include #include #include /* A test ca
因此,玩具程序复制了我使用 cmocka 为现有代码开发单元测试时遇到的问题。问题是嵌套函数调用不模拟,这使得单元测试依赖于嵌套函数调用的正确执行。请注意,使用“mockable_static”定义是
我正在尝试在 Mac OSX Yosemite 10.10.3 上安装并运行 cmocka 库进行单元测试,但是我在 RPATH 方面遇到了一些问题设置。 更新: 感谢@baf,我能够像这样手动将 c
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我是一名优秀的程序员,十分优秀!