我正在开展一个using the cmocka framework的项目。 cmocka主页说明
测试夹具是设置和拆卸功能,可以在多个测试用例之间共享,以提供准备测试环境并在之后销毁它的常用功能。
然而,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有什么功能(如果有的话)让我访问在所述灯具中创建的东西?
答案 0 :(得分:3)
这是一个模板单元测试文件,您可以在项目中重复使用。它解决了您的所有要求
#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编译技巧是这些模拟正常工作的必要条件
答案 1 :(得分:1)
您传递给ws.Cells(lr + 1, 1).Value = lr - 1 + 1000
ws.Cells(lr + 1, 2).Value = tbNewSourceName.Text
函数的那两个NULL应该是cmocka_run_group_tests
和group_setup
函数,都是group_teardown
类型 - 如果您想要使用
可在多个测试用例之间共享的设置和拆卸功能
在cmocka中名为测试灯具。这个函数的documentation表示。
然后在每个测试中都可以访问共享的(void ** state)。它可以在下面的示例中使用:
CMFixtureFunction
哪个应该产生类似
的输出#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cmocka.h>
#define GREETINGS "one string to rule them all, One string to find them, "\
"one string to bring them all and in the darkness bind them"
#define PRECIOUSS_T1 "Three strings for the Elven-kings under the sky,"
#define PRECIOUSS_T2 "even for the Dwarf-lords in halls of stone,"
#define PRECIOUSS_T3 "Nine for Mortal Men, doomed to die,"
#define PRECIOUSS_T4 "One for the Dark Lord on his dark throne"
#define PRECIOUSS_T5 "In the Land of Mordor where the Shadows lie."
#define PRECIOUSS_T6 "One string to rule them all, One Ring to find them,"
#define PRECIOUSS_T7 "One string to bring them all and in the darkness bind them."
#define PRECIOUSS_T8 "In the Land of Mordor where the Shadows lie."
#define OOPS "Not quite what I expected"
#define T(X) PRECIOUSS_T##X
#define FOO(X) case X: strncpy(lots->memory, T(X), sizeof(T(X))); break;
#define SPR_FOO(X) case X: assert_string_equal(T(X), lots->memory); break;
typedef struct {
int line;
char * memory;
} lord_of_the_strings_t;
static int gr_setup(void **state) {
/* this is run once before all group tests */
lord_of_the_strings_t *book = malloc(sizeof(*book));
if (book == NULL)
return -1;
book->memory = malloc(sizeof(GREETINGS));
if (book->memory == NULL) {
return -1;
}
strncpy(book->memory, GREETINGS, sizeof(GREETINGS));
assert_string_equal(book->memory, GREETINGS);
*state = book;
return 0;
}
static int gr_teardown(void **state) {
/* this is run once after all group tests */
lord_of_the_strings_t *lots = *state;
free(lots->memory);
free(lots);
return 0;
}
static int ve_teardown(void **state) {
/* this is run before some single tests */
lord_of_the_strings_t *lots = *state;
lots->line = 42;
return 0;
}
static int ve_setup(void **state) {
/* this is run after some single tests */
static int setup_counter = 0;
lord_of_the_strings_t *lots = *state;
lots->line = ++setup_counter;
switch (setup_counter) {
FOO(1)
FOO(2)
FOO(3)
FOO(4)
FOO(5)
FOO(6)
FOO(7)
FOO(8)
default:
strncpy(lots->memory, OOPS, sizeof(OOPS));
};
return 0;
}
static void failing_test(void **state) {
assert_false("Sorry");
}
static void line_aware_test(void **state) {
lord_of_the_strings_t *lots = *state;
printf(" (shared) between tests, line=%d memory=%s\n", lots->line, lots->memory);
}
static void passing_test(void **state) {
}
static void string_recite_test(void **state) {
static int line_counter = 0;
lord_of_the_strings_t *lots = *state;
if (lots->line < 9)
assert_true(line_counter+1 == lots->line);
switch (++line_counter) {
SPR_FOO(1)
SPR_FOO(2)
SPR_FOO(3)
SPR_FOO(4)
SPR_FOO(5)
SPR_FOO(6)
SPR_FOO(7)
SPR_FOO(8)
default:
line_counter = 0;
}
}
int main(void) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(passing_test),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test(line_aware_test),
cmocka_unit_test(line_aware_test),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test(line_aware_test),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test(line_aware_test),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test_setup_teardown(string_recite_test, ve_setup, ve_teardown),
cmocka_unit_test(failing_test),
};
return cmocka_run_group_tests(tests, gr_setup, gr_teardown);
}