用gtester做单元测试

http://hi.baidu.com/richardma_/blog/item/99a973114bac86dcf7039e84.html


Qemu中用的是这个做测试。


头文件:

#ifndef __CRIEDT_H__
#define __CRIEDT_H__

int criedt(int c);

#endif /* __CRIEDT_H__ */


被测试文件(函数):

#include "criedt.h"

int criedt(int c)
{
if (c > 500) return 1;
else if (c > 300 && c <= 500) return 2;
else if (c > 150 && c <= 300) return 3;
else if (c>0 && c<= 150) return 4;
else return 0;
}


测试文件:

#include <glib.h>
#include "criedt.h"

static void test_500(void)
{
int test;
test = 600;
g_assert_cmpint(criedt(test), ==, 1);
test = 501;
g_assert_cmpint(criedt(test), ==, 1);
test = 500;
g_assert_cmpint(criedt(test), ==, 2);
test = 499;
g_assert_cmpint(criedt(test), ==, 2);
}

static void test_300(void)
{
int test;
test = 301;
g_assert_cmpint(criedt(test), ==, 2);
test = 300;
g_assert_cmpint(criedt(test), ==, 3);
test = 299;
g_assert_cmpint(criedt(test), ==, 3);
}

static void test_150(void)
{
int test;
test = 151;
g_assert_cmpint(criedt(test), ==, 3);
test = 150;
g_assert_cmpint(criedt(test), ==, 4);
test = 149;
g_assert_cmpint(criedt(test), ==, 4);
}

static void test_0(void)
{
int test;
test = 1;
g_assert_cmpint(criedt(test), ==, 4);
test = 0;
g_assert_cmpint(criedt(test), ==, 0);
test = -1;
g_assert_cmpint(criedt(test), ==, 0);
}

int main(int argc, char *argv[])
{
/* 初始化测试框架,必须传入NULL作为结束 */
g_test_init(&argc, &argv, NULL);

/* 注册测试函数,在测试过程中,这些函数将按顺序执行 */
g_test_add_func("/criedt/500", test_500);
g_test_add_func("/criedt/300", test_300);
g_test_add_func("/criedt/150", test_150);
g_test_add_func("/criedt/0", test_0);

/* 运行测试 */
return g_test_run();
}


编译:

gcc -c -o criedt.o  criedt.c

gcc -o gtest `pkg-config --cflags --libs glib-2.0` criedt.o  gtest.c


运行测试:

gtester -k --verbose  -o=test.log ./gtest
TEST: ./gtest... (pid=20690)
  /criedt/500:                                                         OK
  /criedt/300:                                                         OK
  /criedt/150:                                                         OK
  /criedt/0:                                                           OK
PASS: ./gtest

生成html的报告:

gtest-report test.log > test-report.html


蛮好的一个工具。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值