for (before-all; condition; after-each) { }
Cmockery是google开源的一套C语言单元测试框架。
正如编写这个框架的动机,选择这个测试框架的原因在于 兼容性好 、耦合度低 、不依赖外部库 。总得来说,就是简单!
按照功能,可分为四个模块
测试单元用例是以函数形式给出
void test_function(void** state) { }
然后,将其添加到一个测试单元组当中,测试单元组为一个数组
const UnitTest tests = { unit_test(test_function), };
运行测试
run_tests()
如果出现异常,则程序会中断测试。如果发生错误,输出相关信息后继续执行下一条单元测试用例。
expect_assert_failure():类似于C语言中的assert assert_{type}_equal(), assert_{type}_equal:用于检查运行结果。 expect_{type}(),用于函数输入参数和测试用例的检查
用于宏定义的方式重新定义C运行库的内存管理函数,通过在申请内存时,加入信息头记录在链表当中的方式检测内存泄漏。
//1 int* need_to_mock() { //2 return (int*)mock(); } int* want_to_test_func() { return need_to_mock(); void test_function(void** state) { //3 will_return(want_to_test_func, 0x2046); assert_true(want_to_test_func() == 1); } int main(int, char**) { const UnitTest u[] = { unit_test(test_function), } return run_tests(u); }
通过unit_test_setup_teardonw可以在单元测试运行之前改变(预装载)和恢复环境状态(测试后卸载)。
this is my fork that fixes those problems above
在[]之中的字符串集合可以更好的格式化字符串 a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string.
[^characters] 匹配直到所有非此集合中的字符,直到此集合的字符出现为止。If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.
[characters] 匹配直到出现未在此集合中出现的字符input field is read up to the first character that does not appear in the control string
int main(int argc, char * * a rgv) { char* s = "key= value "; char key[1024] = {0}; char value[1024] = {0}; char* digits = "pi3.1415926"; sscanf(s, "%[^=]=%[^\0]", key, value); printf("%s\n%s\n", key, value); sscanf(digits, "%[abcdefghijklmnopqrstuvwxyz]%[1234567890.]", key, value); printf("%s\n%s\n", key, value); return 0; }