Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1697478
  • 博文数量: 410
  • 博客积分: 9563
  • 博客等级: 中将
  • 技术积分: 4517
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-03 19:59
个人简介

文章分类

全部博文(410)

文章存档

2017年(6)

2016年(1)

2015年(3)

2014年(4)

2013年(32)

2012年(45)

2011年(179)

2010年(140)

分类: C/C++

2010-07-04 17:59:51

循环测试

    在测试中,经常出现要校验多组值的情况,如果用tcase_add_test() 如果一条测试条件通不过将退出循环测试,tcase_add_loop_test()可以解决这个问题。
    在测试循环内部可以用_i来表示循环计数


static const int primes[5][3]={{2,3,5},
                                {1,2,3},
                                {7,9,16},
                                {2,2,4},
                                {5,6,10}};
START_TEST(test_loop_add)
{
     fail_unless(add(primes[_i][0],primes[_i][1]) == primes[_i][2], "add函数内部计算错误");
}
END_TEST


在Suite *make_add_suite(void)函数中增加一句

tcase_add_loop_test(tc_add,test_loop_add,0,5);

运行./myprog 输出结果:


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},
                                {1,2,3},
                                {7,9,16},
                                {2,2,4},
                                {5,6,11},
                                {2,5,7}
                            };
START_TEST(test_loop_add)
{
     fail_unless(add(primes[_i][0],primes[_i][1]) == primes[_i][2], "add函数内部计算错误");
}
END_TEST

Suite *make_add_suite(void)
{
        Suite *s = suite_create("Add");//建立测试套件

        TCase *tc_add = tcase_create("add");//建立测试用例集

       tcase_add_loop_test(tc_add,test_loop_add,0,sizeof(primes)/sizeof(primes[0]));

        suite_add_tcase(s, tc_add);//把测试用例集加入到套件中

        return s;


(原文参考)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++)
{
fork(); /* New context */
tfun->f(_i); /* Call test function */
wait(); /* Wait for child to terminate */
}

An example of looping test usage follows:

 
static const int primes[5] = {2,3,5,7,11};

START_TEST (check_is_prime)
{
fail_unless (is_prime (primes[_i]));
}
END_TEST

...

tcase_add_loop_test (tcase, check_is_prime, 0, 5);

Looping tests work in CK_NOFORK mode as well, but without the forking. This means that only the first error will be shown.

阅读(1695) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~