单元测试check笔记(二)
时间:2010-07-04 来源:bailiangcn
循环测试
在测试中,经常出现要校验多组值的情况,如果用tcase_add_test() 如果一条测试条件通不过将退出循环测试,用tcase_add_loop_test()可以解决这个问题。在测试循环内部可以用_i来表示循环计数
static const int primes[5][3]={{2,3,5}, |
在Suite *make_add_suite(void)函数中增加一句
tcase_add_loop_test(tc_add,test_loop_add,0,5); |
85%: Checks: 7, Failures: 1, Errors: 0 unit_test/test_add.c:40:P:add:test_add:0: Passed unit_test/test_add.c:57:P:add:test_sub:0: Passed unit_test/test_add.c:51:P:add:test_loop_add:0: Passed unit_test/test_add.c:51:P:add:test_loop_add:1: Passed unit_test/test_add.c:51:P:add:test_loop_add:2: Passed unit_test/test_add.c:51:P:add:test_loop_add:3: Passed unit_test/test_add.c:51:F:add:test_loop_add:4: add函数内部计算错误 |
适应性更好的代码
static const int primes[][3]={{2,3,5},
tcase_add_loop_test(tc_add,test_loop_add,0,sizeof(primes)/sizeof(primes[0])); |
(原文参考)4.6 Looping Tests
Looping tests are tests that are called with a new context for each loop iteration. This makes them ideal for table based tests. If loops are used inside ordinary tests to test multiple values, only the first error will be shown before the test exits. However, looping tests allow for all errors to be shown at once, which can help out with debugging.
Adding a normal test with tcase_add_loop_test() instead of tcase_add_test() will make the test function the body of a for loop, with the addition of a fork before each call. The loop variable _i is available for use inside the test function; for example, it could serve as an index into a table. For failures, the iteration which caused the failure is available in error messages and logs.
Start and end values for the loop are supplied when adding the test. The values are used as in a normal for loop. Below is some pseudo-code to show the concept:
for (_i = tfun->loop_start; _i < tfun->loop_end; _i++) |
An example of looping test usage follows:
static const int primes[5] = {2,3,5,7,11}; |
Looping tests work in CK_NOFORK mode as well, but without the forking. This means that only the first error will be shown.