Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1109868
  • 博文数量: 254
  • 博客积分: 1242
  • 博客等级: 少尉
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-03 21:49
文章分类

全部博文(254)

文章存档

2017年(16)

2016年(4)

2013年(94)

2012年(140)

分类: C/C++

2013-04-24 20:58:11

check入门教学文档很好,但是例子源代码有错误,而且没有提供make文件,我调试了一下,贴出来
目录结构:
|---makefile
|---unit_test
|        |---test_add.c
|        |---test_main.c
|---add
|    |---add.c
|---include
|      |---uni_test.h
|      |---add.h


makefile

vpath %.h include
vpath %.c add
vpath %.c unit_test

objects = add.o test_add.o
myprog: test_main.c $(objects)
    gcc -I include -lcheck $^ -o myprog

all: $(objects)
$(objects): %.o: %.c
    gcc -c -I include $< -o $@

add.o: add.h

.PHONY: clean
clean:
    rm *.o


filename:test_add.c

#include "check.h"
#include "uni_test.h"
START_TEST(test_add)
{
 fail_unless(add(2, 3) == 5, "god, 2+3!=5");
}
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);//把我们的测试用例加入到测试集中


        return s;
}


filename:test_main.c


#include "uni_test.h"
#include <stdlib.h>

int main(void)
{
        int n;
        SRunner *sr;

        sr = srunner_create(make_add_suite());//把Suite加入到SRunner里面


        srunner_run_all(sr, CK_NORMAL);//运行所有测试用例


        n = srunner_ntests_failed(sr);
        srunner_free(sr);

        return (n==0)? EXIT_SUCCESS: EXIT_FAILURE;
}


filename:add.c

#include "add.h"
int add(int i, int j)
{
 return 0;
}


filename:uni_test.h

#ifndef _UNIT_TEST_H
#define _UNIT_TEST_H

#include "check.h"
Suite *make_add_suite(void);

#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif


filename:add.h

#ifndef _ADD_H
#define _ADD_H

int add(int i, int j);

#ifdef __cplusplus
extern "C" {
#endif


#ifdef __cplusplus
}
#endif

#endif


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