CUnit使用初体验

1. 源码下载 

https://sourceforge.net/projects/cunit/

2. 安装

tar  -jxvf  CUnit-2.1-3.tar.bz2

cd  CUnit-2.1-3

 libtoolize -f -c -i

mv configure.in configure.ac

aclocal

 autoconf

autoheader

automake  --add-missing

automake

chmod u+x configure

 ./configure -prefix=/opt/cunit

make

make install

3. 编写

 env.ini

export  LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/cunit/lib/

Makefile

all:
                gcc -o test main.c -I/opt/cunit/include -L/opt/cunit/lib -lcunit

main.c

#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <CUnit/Automated.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <CUnit/Console.h>
#include <CUnit/TestDB.h>
#include <assert.h>

int suite_success_init(void){
    return 0;
}

int suite_success_clean(void){
    return 0;
}
void test_fun1(void){
        printf("hello cunit\n");
}
void test_fun2(void){
        //测试函数编写
}
CU_TestInfo test_cases1[] = {
    {"test1", test_fun1},
    {"test2", test_fun2},
    CU_TEST_INFO_NULL
};
CU_TestInfo test_cases2[] = {
    CU_TEST_INFO_NULL
};

CU_SuiteInfo suites[] = {
    {"test_cases1:", suite_success_init, suite_success_clean, NULL, NULL, test_cases1},
    {"test_cases2:", suite_success_init, suite_success_clean, NULL, NULL, test_cases2},
    CU_SUITE_INFO_NULL
};

int main(int argc, char **argv)
{
    if (CU_initialize_registry()) {
        fprintf(stderr, " Initialization of Test Registry failed. ");
        exit(EXIT_FAILURE);
    } else {
                assert(NULL != CU_get_registry());
                assert(!CU_is_test_running());
                if (CUE_SUCCESS != CU_register_suites(suites)) {
                        exit(EXIT_FAILURE);
                }
#if 0
                /**** Automated Mode *****************/
                CU_set_output_filename("TestMax");
                CU_automated_run_tests();
                CU_list_tests_to_file();
#else
                //***** Basice Mode *******************
                CU_basic_set_mode(CU_BRM_VERBOSE);
                CU_basic_run_tests();
#endif
        CU_cleanup_registry();
        return CU_get_error();
    }
    return 0;
}

5. 执行

 source env.ini

make

./test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值