yelinqing 发表于 2013-2-1 12:00:10

最简单的C语言单元测试框架

资料来自Internet。最简单的C语言单元测试框架,只有一个3行的头文件。如下:
 
$ cat test/mini_test.h
#define mini_assert(message, test) do { if (!(test)) return message; } while (0)
#define mini_test(test) do { char *message = test(); tests_run++; if (message) return message; } while (0)
extern int tests_run;
 
使用方法:
$ cat test/test_all.c
 

<span style="color: #99cc00;">#include <stdio.h>  /*For printf*/
#include "mini_test.h"
 
int tests_run =0;
static char *test1() {
    mini_assert("must be true", 1==1);
    return 0;
}
static char *all_tests() {
    mini_test(test1);
    return 0;
}
 
int main(int argc, char **argv) {
    char *result = all_tests();
    if (result != 0) {
        printf("%s\n", result);
    } else {
        printf("ALL TESTS PASSED\n");
    }
    printf("Tests run: %d\n", tests_run);
    return result != 0;
}
页: [1]
查看完整版本: 最简单的C语言单元测试框架