Cunit的使用

CUnit的下载地址:CUnit下载

本文使用的是CUnit-2.1-3.tar.bz2

安装步骤

tar jxvf CUnit-2.1-3.tar.bz2
cd CUnit-2.1-3
./bootstrap
./configure --prefix=/opt/cunit/local 
make
make install

举例

文件组织结构

  1. 被测试的模块
    (1)文件ap_math.c,定义了一个求和函数:int cal_num(int a, int b);
    (2)头文件ap_math.h
  2. 测试用例和测试包
    (1)文件test_ap_math.c定义了测试用例
  3. 单元测试入口
    (1)文件test_run.c

源代码

文件ap_math.c
#include "ap_math.h"
int cal_num(int a, int b)
{
   int c;  
   c = a + b;
   return c;
}
头文件ap_math.h
#ifndef AP_MATH_H
#define AP_MATH_H

int cal_num(int a, int b);

#endif
文件test_ap_math.c
#include <stdio.h>
#include <assert.h>
#include "Console.h"
#include "ap_math.h"

int init_suite()
{
    return 0;
}

int end_suite()
{
    return 0;
}

int test_is_euqal(int a, int b, int real)
{
    int result;
    result = cal_num(a, b);
    if (result == real)
    {
        return 1;
    }
    return 0;
}

int test_is_not_equal(int a, int b, int real)
{
    int result;
    result = cal_num(a, b);
    if (result != real)
    {
        return 1;
    }
    return 0;

}

void test1()
{
    CU_ASSERT(test_is_euqal(3, 4, 7));

}

void test2()
{
    CU_ASSERT(test_is_not_equal(3, 4 ,10));
}

int test_cal_module()
{
    CU_pSuite p_suite = NULL;
    p_suite = CU_add_suite("test_suite", init_suite, end_suite);
    if (p_suite == NULL)
    {
        //return 1;
    }

    if (CU_add_test(p_suite, "test1", test1) == NULL
            || CU_add_test(p_suite, "test2", test2) == NULL)
    {
        return 1;
    }
    return 0;
}
文件test_run.c
#include <stdio.h>
#include <assert.h>

#include "Console.h"

extern int test_cal_module ();

int main(int argc, char const *argv[])
{
    if (CU_initialize_registry() != CUE_SUCCESS)
    {
        return CU_get_error();
    }
    assert(CU_get_registry() != NULL);
    assert(!CU_is_test_running());
    if (test_cal_module() != 0)
    {
        CU_cleanup_registry();
        return CU_get_error();
    }

    CU_console_run_tests();

    CU_cleanup_registry();
    return 0;
}
Makefile文件
all: test

CC = gcc
CFLAGS = -c -Wall

OBJS = ap_math.o \
        test_ap_math.o \
        test_run.o

CUNITDIR = /opt/cunit/local

INCS = -I $(CUNITDIR)/include/CUnit -L $(CUNITDIR)/lib

LIBS = -lcunit -lcurses -static

test: $(OBJS)
    $(CC) $(INCS) $^ $(LIBS)  -o $@

%.o: %.c
    $(CC) $(CFLAGS) $(INCS) $< -o  $@ 

clean:
    rm -f $(OBJS) test

运行

Linux下输入

./test即可看到console命令界面

参考:
http://blog.csdn.net/scucj/article/details/4385630

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值