文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>单元测试check笔记(二)

单元测试check笔记(二)

时间:2010-07-04  来源:bailiangcn

循环测试

    在测试中,经常出现要校验多组值的情况,如果用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.

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载