c语言单元测试框架check

什么是check

最近学习的开源代码中,测试代码都是依赖了一个叫做check库编写的,调查之后才知道,这个check是个单元测试框架。下面对它进行简单的介绍。

Check 最主要的优点是对于每一个测试用例的运行都 fork 一个子进程,这么做的原因是因为 C 语言的独特性:
(1) 其它语言如 Java,Python,Ruby等,单元测试出错最多不过是抛出异常;
(2) C 语言如果指针操作错误,乱指一气,可是会 coredump的。测试框架因此直接退出,用户是看不到任何返回的,只有郁闷的 coredump;
(3) Check 的单元测试运行在 fork 的子进程中,可以避免测试框架由于 coredump 而崩溃。

ubuntu下安装check库

check官网

sudo apt-get install check

Github地址:GIthub

Check使用

从一个简单的工程开始说明。

使用tree查看目录机构:

.
├── include
│   ├── sub.h
│   └── unit_test.h
├── makefile
├── sub
│   └── sub.c
└── unit_test
    ├── test_main.c
    └── test_sub.c

sub.c文件

#include "sub.h"

int sub(int a, int b) {
    return a-b;
}

sub.h文件

#ifndef _SUB_H
#define _SUB_H
int sub(int a, int b);
#endif

unit_test.h文件

#ifndef _UNI_TEST_H
#define _UNI_TEST_H
#include "check.h"
Suite *make_sub_suite(void);
#endif

test_sub.c文件

#include "check.h"
#include "unit_test.h"
#include "sub.h"
START_TEST(test_sub) {
    fail_unless(sub(6, 2) == 4, "error, 6 - 2 != 4"); 
}
END_TEST

Suite * make_sub_suite(void) {
    Suite *s = suite_create("sub");       // 建立Suite
    TCase *tc_sub = tcase_create("sub");  // 建立测试用例集
    suite_add_tcase(s, tc_sub);           // 将测试用例加到Suite中
    tcase_add_test(tc_sub, test_sub);     // 测试用例加到测试集中
    return s;
}

test_main.c文件

#include "unit_test.h"
#include 

int main(void) {
    int n, n1;
    SRunner *sr, *sr1;
    sr = srunner_create(make_sub_suite()); // 将Suite加入到SRunner
    srunner_run_all(sr, CK_NORMAL);
    n = srunner_ntests_failed(sr);         // 运行所有测试用例
    srunner_free(sr);
    return (n == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

makefile文件

vpath %.h include  #vpath 指定搜索路径
    vpath %.c add
    vpath %.c unit_test

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

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

    .PHONY: clean
    clean:
        rm *.o test

运行结果

等待验证O(∩_∩)O哈哈~

猜你喜欢

转载自blog.csdn.net/weixin_33747129/article/details/87449849