Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1689851
  • 博文数量: 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 12:02:42

参照前面的文档,继续单元测试check的试验()
试验1:(增加一个新的函数)
    在add.c中增加一个sub函数,新的add.c

int sub(int i, int j)
{
    int res=0;
    res=i-j;
    return res;
}

    在add.h中增加sub函数的定义

int sub(int i, int j);

编译一下,通过了,但是没有运行测试,现在增加一个测试,test_main.c不用动,需要修改test_add.c文件,修改后变成这样(红色是新加的语句)

#include "check.h"
#include "uni_test.h"
START_TEST(test_add)
{
 fail_unless(add(2, 3) == 5, "god, 2+3!=5");
 fail_unless(add(4,4 ) == 8, "god, 4+4!=8");
}
END_TEST
START_TEST(test_sub)
{
 fail_unless(sub(2, 3) == -1, "god, 2-3!=-1");
}
END_TEST

Suite *make_add_suite(void)
{
        Suite *s = suite_create("Add");//建立测试套件(我不知道,这么翻译对不对?^_^)

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


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

        tcase_add_test(tc_add, test_add);//把我们的测试用例加入到测试集中

        tcase_add_test(tc_add, test_sub);//新增加的测试用例

        return s;
}

重新编译,运行,结果如下

Running suite(s): Add

100%: Checks: 2, Failures: 0, Errors: 0


基础知识:


        fail_unless(判断条件,输出字符串) //如果判断条件不成立,输出字符串
        fail_if(判断条件,输出字符串)//与fail_unless正相反,输出字符串可以是NULL
            输出字符串支持可打印格式

例句:

fail_unless(add(2, 3) == 5, "god, 2+3!=5");
fail_if(add(4,4 ) == 8, NULL);
fail_unless(sub(2, 3) == -1, "god, 2-3!=-1");

fail_if(add(4,4 ) != 8, NULL);
fail_unless(add(5,6) == 10, "add() 发生错误,结果应当%d",add(5,6));


运行测试用例的函数定义(在例子test_main.c中):

 
void srunner_run_all (SRunner * sr, enum print_output print_mode);

这个函数做2个事情:

  1. 运行所有测试用例,并收集结果
  2. 根据print_mode输出结果

另外一个输入函数:当 SRunners 已经运行过了,可以用srunner_print输出结果

 
void srunner_print (SRunner *sr, enum print_output print_mode);

输出结果的样式是个枚举值,在check.h中定义了

CK_SILENT

没有输出

CK_MINIMAL

摘要输出(number run, passed, failed, errors).

CK_NORMAL

正常输出,打印摘要和每条错误信息

CK_VERBOSE

详细输出,打印摘要和每条测试结果

CK_ENV

从环境变量取得输出样式

Gets the print mode from the environment variable CK_VERBOSITY, which can have the values "silent", "minimal", "normal", "verbose". If the variable is not found or the value is not recognized, the print mode is set to CK_NORMAL.

CK_SUBUNIT

Prints running progress through the test runner protocol. See 'subunit support' under the Advanced Features section for more information.

试验二:(setup() and teardown())准备环境

      可以利用这两个函数实现测试用例的环境准备,控制流的顺序
       
unchecked_setup();
fork();
checked_setup();
check_one();//(用例1)
checked_teardown();
wait();
fork();
checked_setup();
check_two();//用例2
checked_teardown();
wait();
unchecked_teardown();

使用 tcase_add_checked_fixture()加入测试用例集

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