cunit单元测试

2023-11-19

一,安装cunit

1.1下载cunit

https:// sourceforge .NET / projects / cunit

下载最新安装包

1.2解压缩安装包

root@Ubuntu1610:/# tar jxvf CUnit-2.1-3.tar.bz2

1.3进入解压后的文件夹

/*修改名称*/
Mv CUnit-2.1-3 CUnit
/*进入文件夹*/
cd CUnit

1.4执行make,分别执行以下命令

root@Ubuntu1610:/usr/local/include/CUnit# aclocal

root@Ubuntu1610:/usr/local/include/CUnit# autoheader

root@Ubuntu1610:/usr/local/include/CUnit# autoconf

root@Ubuntu1610:/usr/local/include/CUnit# automake

如果在automake的过程中有文件丢失,则执行下面命令:

root@Ubuntu1610:/usr/local/include/CUnit# automake --add-missing

再次执行的automake

如果出现错误:configure.in:161:找不到所需文件'./ltmain.sh',则执行下面的命令:

root@Ubuntu1610:/usr/local/include/CUnit# libtoolize --automake --copy --debug --force

执行完上述步骤以后,查看是否生成了配置,如果有,则执行:

root@Ubuntu1610:/usr/local/include/CUnit# ./configure

执行完上述步骤以后,查看是否生成了Makefile文件,如果有,则执行:

root@Ubuntu1610:/usr/local/include/CUnit# make

安装编译出的库:

root@Ubuntu1610:/usr/local/include/CUnit# sudo make install

查看是否已经安装成功:

root@Ubuntu1610:/usr/local/include/CUnit# ls /usr/local/lib/libcunit.so

二,cunit测试实例

2.1设置cunit 的头文件和库文件

这个需要根据个人安装地址进行配置,等会需要放到Makefile文件里

2.2配置测试文件

文件架构

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# tree

.

├── cal.c    --被测文件

├── cal.h    --被测文件头文件

├── Makefile

├── test.c   --测试文件

└── testcal.c –测试运行文件



0 directories, 5 files

代码:

cal.h
 

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# cat cal.h




int add(int a, int b );

int minus( int a, int b);

cal.c

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# cat cal.c

#include"cal.h"



int add(int a, int b ){

        return a + b;

}



int minus( int a, int b){

        return a - b;

}

testcal.c

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# cat testcal.c

#include <stdio.h>

#include <assert.h>

#include <CUnit/Console.h>

#include "cal.h"



int InitSuite()

{

        return 0;

}



int EndSuite()

{

        return 0;

}





void TestAdd()

{

//      CU_ASSERT(test_add(3, 4, 7));

  int result = add(3, 5);

  int real = 7;

        CU_ASSERT_EQUAL(result,real);

}



void TestMinus()

{

  //    CU_ASSERT(test_minus(4, 5, -1));

  int result = minus(3, 4);

  int real = -1;

        CU_ASSERT_EQUAL(result,real);

}





/*0 表示成功,1表示失败*/

int AddTestCalModule()

{

        CU_pSuite pSuite = NULL;



        /***************

        * 1. CU_add_suite 增加一个Suite

        * 2. Suite名字 : testSuite

        * 3. InitSuite EndSuite:分别是测试单元初始和释放函数,如不需要则NULL传递

        ****************/

        pSuite = CU_add_suite("cal模块", InitSuite, EndSuite); 



        //检测注册Suite情况

        if(NULL == pSuite)

        {

                //return 1;

        }



        /***************

        * 1. 注册当前Suite下的测试用例 

        * 2. pSuite:用例指针

        * 3. "Test1": 测试单元名称

        * 4. Test1:测试函数

        ***************/

        if( NULL == CU_add_test(pSuite, "testadd()", TestAdd) ||

                NULL == CU_add_test(pSuite, "testminus()", TestMinus))

        {

                return 1;

        }



        /***另外一种测试方式***************/

        /*

        CU_TestInfo testcases[] = {

        {"Test1:", Test1},

        {"Test2:", Test2},

        CU_TEST_INFO_NULL

        };



        CU_SuiteInfo suites[] = {

                {"Testing the function cal_num:", InitSuite, EndSuite, testcases},

        CU_SUITE_INFO_NULL

        };



        if(CUE_SUCCESS != CU_register_suites(suites))

        {

                return 1;

        }

        */

        /************************************/



        return 0;

}

 

test.c的

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# cat test.c

#include <stdio.h>

#include <assert.h>

#include <CUnit/Console.h>

#include "CUnit/Basic.h"



extern int AddTestCalModule();



int main()

{

  

        //CU_initialize_registry עE_ϵ



        if( CUE_SUCCESS != CU_initialize_registry())

        {

                return CU_get_error();

        }



        //CU_get_registry עָ



                                assert(NULL != CU_get_registry());



        //



                assert(!CU_is_test_running());



        //ģ

    printf("%d",AddTestCalModule());

//      if (0 != AddTestCalModule())

//      {

//              CU_cleanup_registry();

//              return CU_get_error();

//      }



        //ʹnsole



                        //CU_console_run_tests();



        /***ʹLʽ********/

        //CU_set_output_filename("TestMax");

   // CU_list_tests_to_file();

        //CU_automated_run_tests();



        CU_basic_set_mode(CU_BRM_VERBOSE);

        CU_basic_run_tests();



        //עϢ

        CU_cleanup_registry();



        return 0;

}

Makefile文件

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# cat Makefile

INC=-I/usr/local/include/CUnit

LIB=-L/usr/local/lib/

#gcc -o test $(INC) $(LIB)  $^ -lcunit *.c

#gcc -o test -I /usr/include/CUnit/ -L /usr/lib64/ -lcunit *.c

all: cal.c testcal.c test.c

        gcc $^ -o hello $(INC) $(LIB) -lcunit

clean:

        rm -rf hello

 

2.3运行

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# make

gcc cal.c testcal.c test.c -o hello -I/usr/local/include/CUnit -L/usr/local/lib/ -lcunit

生成你好文件

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# ls

cal.c  cal.h  hello  Makefile  test.c  testcal.c

 

运行招呼文件,结果:

root@Ubuntu1610:~/.jenkins/workspace/14sDemo-Cunit/cunit-test# ./hello



     CUnit - A unit testing framework for C - Version 2.1-3

     http://cunit.sourceforge.net/

Suite: cal模块

  Test: testadd() ...FAILED

    1. testcal.c:22  - CU_ASSERT_EQUAL(result,real)

  Test: testminus() ...passed



Run Summary:    Type  Total    Ran Passed Failed Inactive

              suites      1      1    n/a      0        0

               tests      2      2      1      1        0

             asserts      2      2      1      1      n/a

Elapsed time =    0.000 seconds

2.4 cunit详解

(1)CUNIT 的架构

按官方文档说明,使用Cunit的主要步骤有:
1) Write functions for tests (and suite init/cleanup if necessary). 
2) Initialize the test registry - CU_initialize_registry() 
3) Add suites to the test registry - CU_add_suite() 
4) Add tests to the suites - CU_add_test() 
5) Run tests using an appropriate interface, e.g. CU_console_run_tests 
6) Cleanup the test registry - CU_cleanup_registry 

(2)测试模式

下面是四种测试模式:
1 Automated Output to xml file            Non-interactive
2 Basic      Flexible programming        interface Non-interactive  
3 Console    Console interface (ansi C)     Interactive  
4 Curses     Graphical interface (Unix)     Interactive
第一种模式是将结果输出到XML文档中,便于生成报告。第二种模式是每一次运行结束之后在standard output中显示测试结果,不能保留测试结果数据。第三种模式是console方式的,可以人机交互;前两种模式是非交互式的。第四种只在Unix中使用。

(3)测试的基本流程
1)编写单元测试函数(有必要的话要写套件的init / cleanup函数)。用于测试的函数(如有必要,还可以进行套件初始化/清理)。 
2)调用函数CU_initialize_registry()初始化测试注册单元(Test Registry)。初始化测试注册表 - CU_initialize_registry() 
3)调用函数CU_add_suite()将测试包(套件)添加到测试注册单元(Test Registry)中。添加套件到测试注册表 - CU_add_suite() 
4)调用函数CU_add_test()将测试用例添加到测试包(套件)中。添加测试到套件 - CU_add_test() 
5)使用合适的接口来运行测试用例。运行测试使用一个合适的接口,例如CU_console_run_tests 
6)调用函数CU_cleanup_registry清除测试注册单元(Test Registry)。清理测试注册表 - CU_cleanup_registry()

测试模式:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cunit单元测试 的相关文章

随机推荐

  • Servlet+JDBC实战开发书店项目讲解第五篇:购物车实现

    Servlet JDBC实战开发书店项目讲解第五篇 购物车实现 引言 在之前的几篇博客中 我们讲解了如何使用Servlet和JDBC开发一个简单的书店管理系统 在本文中 我们将深入探讨购物车的实现 这是一个关键功能 允许用户将所需图书添加到
  • Java的多态特性

    学习笔记 多态 简单说 就是一个对象对应着不同类型 多态在代码中的体现 父类或者接口的引用指向其子类的对象 多态的好处 提高可维护性 由多态前提所保证 提高了代码的扩展性 多态的弊端 无法直接访问子类特有的成员 也就是说前期定义的内容不能使
  • [C++基础]-stack和queue

    前言 作者 小蜗牛向前冲 名言 我可以接受失败 但我不能接受放弃 如果觉的博主的文章还不错的话 还请点赞 收藏 关注 支持博主 如果发现有问题的地方欢迎 大家在评论区指正 目录 一 stack的基本知识 1 什么是栈 2 栈的基本使用 3
  • Python 使用execjs调用网页js 进行数据加密

    最近做一个数据采集项目的时候需要自动采集网站的招投标数据 随便打开一个网站 打开开发者模式 输入关键词 点击搜索 获得以下内容 可以看到请求链接和请求类型 请求类型Content Type 是application x www form u
  • maven 项目导入junit问题

    maven项目无法导入 import org junit Test import org junit runner RunWith 问题 1 检查 Maven Dependencies中Junit的jar中是否有此类 如果没有 说明pom
  • 判断一个IP地址是不是单播地址

    1 组播地址 2 单播地址 1 2
  • C++_生成随机字符串

    include
  • Vite配置跨域代理

    Vite 配置跨域代理 修改vite config js文件 import defineConfig from vite import react from vitejs plugin react https vitejs dev conf
  • Xilinx AXI-memory接口 转 AXI-stream 接口(含源码)

    AXI memory接口 转 AXI stream 接口 AXI memory接口介绍 具体详情可以查看源码 AXI memory接口介绍 从图中我们可以看出memory接口有5个通道 分别是读地址通道 写地址通道 写响应通道 读数据通道
  • 华为OD两轮技术面试

    华为OD面试 1性格测试 选积极向上的选项 注意 性格测试也会挂人 我一个朋友性格测试就没过 2机试 一道变成题目 1h 用例60 通过即可 任给一个数组 元素有20M 1T 300G之类的 其中1T 1000G 1G 1000M 按从小到
  • 数据库事务锁详解

    前言 上篇说到数据库事务中的特性ACID和4个隔离级别 今儿就来看一下事务中的锁 MySQL中的锁 锁是MySQL在服务器层和存储引擎层的并发控制 锁可以保证数据并发访问的一致性 有效性 锁冲突也是影响数据库并发访问性能的一个重要因素 My
  • 并发编程4 - 线程状态、死锁及ReentrantLock

    文章目录 一 再述线程状态转换 二 多把锁与线程活跃性问题 1 多把锁 2 活跃性 三 ReEntrantLock 1 基本用法 2 可重入 3 可打断 4 锁超时 5 公平锁 6 条件变量 一 再述线程状态转换 情况1 New RUNNA
  • JAVA数据结构——利用图的广度优先遍历搜索算法确定无向连通图的连通分量

    分析 如果这个无向图是非连通图的时候 从图的一个顶点没法访问这个图的所有顶点 只能访问包含该顶点的连通分量中的所有顶点 所以从无向图的每个连通分量中的一个顶点开始遍历图 则可求得无向图的所有连同分量 如图则是非连通的无向图 我们只需要从第一
  • (Python笔记)使用Python解析HEX文件的内容

    需要用到binascii库 binascii库中包含了很多在二进制和二进制表示的各种ASCII码之间转换的方法 Code import binascii HEX path r 1 HEX with open HEX path rb as f
  • Python 实现列队

    1 列队定义 队列是项的有序结合 其中添加新项的一端称为队尾 移除项的一端称为队首 当一个元素从队尾进入队列时 一直向队首移动 直到它成为下一个需要移除的元素为止 最近添加的元素必须在队尾等待 集合中存活时间最长的元素在队首 这种排序成为
  • Flutter个推推送Android端,退出应用后收到消息报错

    场景 Android手机 iOS没有测试 1 集成getuiflut 2 返回退出APP 3 发送推送透传消息 报错 Tried to send a platform message to Flutter but FlutterJNI wa
  • (八)nginx反向代理功能

    nginx反向代理概念 反向代理也称reverse proxy 指的就是代理外网用户请求到内部指定web服务器 并将数据返回给用户的一种方式 nginx除了可以在企业提供高性能的web服务之外 另外还可以将本身不具备的请求通过某种预定的协议
  • centos7安装配置fdfs时service fdfs_storaged start启动不成功

    service fdfs storaged start 命令启动后 提示是启动失败 ps ajx grep fdfs 查看不到 fdfs storaged启动信息 解决方法 当时解压缩时 fdfs storaged解压失败 自己当时没注意
  • ERROR:105: Unable to locate a modulefile for 'xxx'

    查看可用的 module module avail 将xxx替换为屏幕输出中已有的模块 转载于 https www cnblogs com zhyantao p 10462141 html
  • cunit单元测试

    一 安装cunit 1 1下载cunit https sourceforge NET projects cunit 下载最新安装包 1 2解压缩安装包 root Ubuntu1610 tar jxvf CUnit 2 1 3 tar bz2