歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Check—強大的C語言單元測試框架

Check—強大的C語言單元測試框架

日期:2017/3/1 9:14:27   编辑:Linux編程

1. 前言

在看基數樹源碼時,發現源碼裡面有個deps的依賴文件夾,deps裡是一個叫Check的源碼安裝包,Google之後發現Check原來是C語言單元測試框架。關於單元測試,Wikipedia的介紹點這裡。

Check 最主要的優點是對於每一個測試用例的運行都 fork 一個子進程,這麼做的原因是因為 C 語言的獨特性:
(1) 其它語言如 Java,Python,Ruby等,單元測試出錯最多不過是拋出異常;
(2) C 語言如果指針操作錯誤,亂指一氣,可是會 coredump的。測試框架因此直接退出,用戶是看不到任何返回的,只有郁悶的 coredump;
(3) Check 的單元測試運行在 fork 的子進程中,可以避免測試框架由於 coredump 而崩潰。

網上介紹Check的使用基本都是同一例子,但如何使用沒有說明,下面就詳細介紹下如何利用Check進行單元測試。


2. Check簡單使用

在使用Check前要安裝Check,可以直接在終端輸入:sudo apt-get install check進行安裝,也可以使用源碼包安裝,源碼具體安裝方法可以參考官網。

我們現在也是要做一個只做加法的工程,整個工程的文件目錄如下圖所示,具體代碼參考Github。工程文件夾裡的TestAdd.c文件是將整個工程都放到了一個文件中,可以直接在終端運行:gcc TestAdd.c -lcheck進行編譯,然後運行./test


TestAdd.c文件

    #include <stdio.h>
    #include "check.h"
    #include <stdlib.h>

    int add(int x1, int x2) {
        return 0;
    }

    START_TEST(test_add) {
        fail_unless(add(2, 3) == 5, "error, 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;
    }

    int main(void) {
        int n;
        SRunner *sr;
        sr = srunner_create(make_add_suite());
        srunner_run_all(sr, CK_NORMAL);
        n = srunner_ntests_failed(sr);
        srunner_free(sr);
        return (n == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    }

下面介紹下工程中的文件夾功能,add目錄下是功能函數,include中是頭文件,測試文件在unit_test文件夾,makefile是整個項目的make文件。

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

add.c文件

    #include "add.h"
    int add(int x1, int x2) {
        return 0;
    }

add.h文件

    #ifndef _ADD_H
    #define _ADD_H
    int add(int x1, int x2);
    #endif

uni_test.h文件

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

test_add.c文件

    #include "check.h"
    #include "uni_test.h"
    #include "add.h"
    START_TEST(test_add) {
        fail_unless(add(2, 3) == 5, "error, 2 + 3 != 5"); // "error, 2 + 3 != 5"是出錯提示信息 
    }
    END_TEST

    Suite * make_add_suite(void) {
        Suite *s = suite_create("add");       // 建立Suite
        TCase *tc_add = tcase_create("add");  // 建立測試用例集
        suite_add_tcase(s, tc_add);           // 將測試用例加到Suite中
        tcase_add_test(tc_add, test_add);     // 測試用例加到測試集中
        return s;
    }

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;
    }

make test 可以編譯生成可執行文件test./test 運行單元測試程序。

【完】

Copyright © Linux教程網 All Rights Reserved