执行测试程序_Test Execution
Cmockery 单元测试程序使用void function(void **state)函数,使用unit_test*()宏定义函数初始化带有测试用例的函数指针的表
#define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
然后将表传给runtest执行
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmockery.h>
// A test case that does nothing and succeeds.
void null_test_success(void **state) {
}
int main(int argc, char* argv[]) {
const UnitTest tests[] = {
unit_test(null_test_success),
};
return run_tests(tests);
}
异常处理_Exception Handling
在 run_tests() 执行测试函数之前,建立一个异常情况处理的程序,程序仅显示错误并在发生异常时退出测试函数。 如果在测试函数之外发生异常,例如在 Cmockery 本身中,应用程序将中止执行并返回错误代码。
错误条件_Failure Conditions
如果通过 run_tests() 执行测试函数期间发生故障,则测试函数将中止,应用程序的执行将继续执行下一个测试函数。测试失败通过 Cmockery 函数 fail() 发出信号。 以下事件将导致 Cmockery 库发布测试失败信号:
Assertions_断言测试
Cmockery中提供以下宏定义断言测试函数:
// Assert that the given expression is true.
assert_true(c)
// Assert that the given expression is false.
assert_false(c)
// Assert that the two given integers are equal, otherwise fail.
assert_int_equal(a, b)
// Assert that the two given integers are not equal, otherwise fail.
assert_int_not_equal(a, b)
// Assert that the two given strings are equal, otherwise fail.
assert_string_equal(a, b)
// Assert that the two given strings are not equal, otherwise fail.
assert_string_not_equal(a, b)
// Assert that the two given areas of memory are equal, otherwise fail.
assert_memory_equal(a, b, size)
// Assert that the two given areas of memory are not equal, otherwise fail.
assert_memory_not_equal(a, b, size)
// Assert that the specified value is >= minimum and <= maximum.
assert_in_range(value, minimum, maximum)
// Assert that the specified value is < minumum or > maximum
assert_not_in_range(value, minimum, maximum)
// Assert that the specified value is within a set.
assert_in_set(value, values, number_of_values)
// Assert that the specified value is not within a set.
assert_not_in_set(value, values, number_of_values)
比如我们assert_in_range断言宏来判断输入数据是否在要求范围之内,编写测试程序如下:
#include "all_head.h"
extern int add_user(int a, int b);
extern int sub_user(int a, int b);
/* Ensure add() adds two integers correctly. */
void test_add(void **state) {
assert_int_equal(add_user(21, 1), 22);
assert_int_equal(add_user(3, -3), 0);
}
/* Ensure sub() subtracts two integers correctly.*/
void test_sub(void **state) {
assert_int_equal(sub_user(3, 3), 0);
assert_int_equal(sub_user(3, -3), 6);
}
int main(int argc, char *argv[])
{
const UnitTest tests[] = {
unit_test(test_add),
unit_test(test_sub),
};
return run_tests(tests);
}
被测函数代码如下:
#include "all_head.h"
//#define NDEBUG 1
#ifdef NDEBUG
#define assert_in_range(expression) ((void)0)
#else
#define assert_in_range(value, minimum, maximum) \
_assert_in_range( \
cast_to_largest_integral_type(value), \
cast_to_largest_integral_type(minimum), \
cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
#endif
int add_user(int a, int b)
{
assert_in_range(a, 1, 20);
return a + b;
}
int sub_user(int a, int b)
{
assert_in_range(a, 1, 20);
return a - b;
}
在断言宏被调用的测试函数所在文件中,我们添加了一个宏定义NDEBUG和一个条件编译的部分。该部分的作用方便我们在使用过程中打开断言宏和关闭断言宏,在我们定义了NDEBUG时,断言宏虽然在程序中,但是并不会被调用。
打开断言宏,运行程序,结果如下:
test_add: Starting test
21 is not within the range 0-1
ERROR: g:\code_training\0811\c_test\c_test\testfunction.c:18 Failure!
test_add: Test failed.
test_sub: Starting test
test_sub: Test completed successfully.
1 out of 2 tests failed!
test_add
请按任意键继续. . .
关闭断言宏的时候,运行代码,结果如下:
test_add: Starting test
test_add: Test completed successfully.
test_sub: Starting test
test_sub: Test completed successfully.
All 2 tests passed
请按任意键继续. . .
|