CUnit例子

2023-10-26

关于CUnit的安装请自行百度

我的系统:fedora22 64bit

我的CUnit的头文件在:/usr/include/CUnit/

库文件在:/usr/lib64/


文件:


[laolang@laolang mycunittest]$ tree
.
├── cal.c
├── cal.h
├── Makefile
├── test.c
└── testcal.c

0 directories, 5 files
[laolang@laolang mycunittest]$
代码:


cal.h


[laolang@laolang mycunittest]$ cat cal.h
#ifndef _CAL_H_
#define _CAL_H_

int add(int a, int b );

int minus( int a, int b);
#endif
[laolang@laolang mycunittest]$
cal.c



[laolang@laolang mycunittest]$ cat cal.c
#include"cal.h"

int add(int a, int b ){
	return a + b;
}

int minus( int a, int b){
	return a - b;
}
[laolang@laolang mycunittest]$
testcal.c



[laolang@laolang mycunittest]$ cat testcal.c 
#include <stdio.h>
#include <assert.h>
#include <CUnit/Console.h>
#include "cal.h"

int InitSuite()
{
	return 0;
} 

int EndSuite()
{
	return 0;
}

int test_add(int a, int b, int real)
{
	int result;

	result = add(a, b);
	if(result == real)
	{
		return 1;
	}
	return 0;
}

int test_minus(int a, int b, int real)
{
	int result;

	result = minus(a, b);
	if(result == real)
	{
		return 1;
	}
	return 0;
}

void TestAdd()
{
//	CU_ASSERT(test_add(3, 4, 7));
  int result = add(3, 4);
  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, "add(加)", TestAdd) ||
		NULL == CU_add_test(pSuite, "minus(减)", 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;
}
[laolang@laolang mycunittest]$
test.c




[laolang@laolang mycunittest]$ cat test.c
#include <stdio.h>
#include <assert.h>
#include <CUnit/Console.h>

extern int AddTestCalModule();

int main()
{
   
	//CU_initialize_registry 注册函数注册一个用例返回CUE_系列异常值
	if( CUE_SUCCESS != CU_initialize_registry())
	{
		return CU_get_error();
	}

	//CU_get_registry 返回注册到用例指针 
	assert(NULL != CU_get_registry());
	
	//检测是否在执行 
	assert(!CU_is_test_running()); 

	//调用测试模块完成测试用例
	if (0 != AddTestCalModule())
	{
		CU_cleanup_registry();
		return CU_get_error();
	}

	//使用console控制交互界面的函数入口 
	CU_console_run_tests();

	/***使用自动产生XML文件的模式********
	CU_set_output_filename("TestMax");
    CU_list_tests_to_file();
	CU_automated_run_tests();
	***********************************/
	
	//调用完毕清除注册信息 
	CU_cleanup_registry();
	
	return 0;
}
[laolang@laolang mycunittest]$





Makefile


laolang@laolang mycunittest]$ cat Makefile 
INC=-I/usr/include/CUnit/
LIB=-L/usr/lib64/
#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
[laolang@laolang mycunittest]$




运行:


[laolang@laolang mycunittest]$ make
gcc cal.c testcal.c test.c -o hello -I/usr/include/CUnit/ -L/usr/lib64/ -lcunit
[laolang@laolang mycunittest]$ ./hello 


     CUnit - A Unit testing framework for C - Version 2.1-3
             http://cunit.sourceforge.net/


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: r

Running Suite : cal模块
     Running Test : add(加)
     Running Test : minus(减)

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      2      2      2      0        0
             asserts      2      2      2      0      n/a

Elapsed time =    0.000 seconds


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: l

--------------------- Registered Suites -----------------------------
 #  Suite Name                         Init? Cleanup? #Tests Active?

 1. cal模块                           Yes      Yes      2     Yes
---------------------------------------------------------------------
Total Number of Suites : 1


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: s

--------------------- Registered Suites -----------------------------
 #  Suite Name                         Init? Cleanup? #Tests Active?

 1. cal模块                           Yes      Yes      2     Yes
---------------------------------------------------------------------
Total Number of Suites : 1

Enter number of suite to select (1-1) : 1-1
Suite 'cal模块' selected.

***************** CUNIT CONSOLE - SUITE MENU ***************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (Q)uit
Enter command: r

Running Suite : cal模块
     Running Test : add(加)
     Running Test : minus(减)

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      2      2      2      0        0
             asserts      2      2      2      0      n/a

Elapsed time =    0.000 seconds

***************** CUNIT CONSOLE - SUITE MENU ***************************
(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (Q)uit
Enter command: u


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: q
[laolang@laolang mycunittest]$




解释:

这个例子参考自:http://blog.csdn.net/scucj/article/details/4385630/

原文用的方法是我注释的那一部分,即写一个test_add方法,再写一个TestAdd方法。不过我觉得这种不如直接在TestAdd方法中调用需要测试的方法

关于CUnit解释的比较好的例子:http://www.cnblogs.com/linux-sir/archive/2012/08/25/2654557.html

然后这个是不能运行的,提示是testcase类型有问题,但是这篇博文的解释还是很好的

我的总结:

1、写你的模块,比如cal

2、每一个模块写一个测试文件,如testcal.c

3、写一个总的测试文件,在此文件中包含需要测试的模块对应的测试文件


现在我把其中的数据修改一下:


void TestMinus()
{
  //	CU_ASSERT(test_minus(4, 5, -1));
  int result = minus(3, 4);
  int real = 1;//这里由-1修改为1
  CU_ASSERT_EQUAL(result,real);
}
运行效果:



[laolang@laolang mycunittest]$ make
gcc cal.c testcal.c test.c -o hello -I/usr/include/CUnit/ -L/usr/lib64/ -lcunit
[laolang@laolang mycunittest]$ ./hello 


     CUnit - A Unit testing framework for C - Version 2.1-3
             http://cunit.sourceforge.net/


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: r

Running Suite : cal模块
     Running Test : add(加)
     Running Test : minus(减)

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


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: f

--------------- Test Run Failures -------------------------
   src_file:line# : (suite:test) : failure_condition

1. testcal.c:53 : (cal模块 : minus(减)) : CU_ASSERT_EQUAL(result,real)
-----------------------------------------------------------
Total Number of Failures : 1


***************** CUNIT CONSOLE - MAIN MENU ******************************
(R)un  (S)elect  (L)ist  (A)ctivate  (F)ailures  (O)ptions  (H)elp  (Q)uit
Enter command: q
[laolang@laolang mycunittest]$




可以看到如果测试未通过,按F可以看出来是那个测试模块的哪个测试出问题了,还提示了在哪一行,哪一个断言







转载于:https://my.oschina.net/iamhere/blog/521558

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

CUnit例子 的相关文章

  • (discord.py) 尝试更改成员角色时,“用户”对象没有属性“角色”

    因此 我正在尝试编写一个机器人 让某人在命令中指定的主持人指定的一段时间内暂停角色 我知道该变量称为 小时 即使它目前以秒为单位 我稍后会解决这个问题 基本上 它是由主持人在消息 暂停 personmention numberofhours
  • 使用Python开发Web应用程序

    我一直在用 python 做一些工作 但这都是针对独立应用程序的 我很想知道 python 的任何分支是否支持 Web 开发 有人还会建议一个好的教程或网站吗 我可以从中学习一些使用 python 进行 Web 开发的基础知识 既然大家都说
  • Python PAM 模块的安全问题?

    我有兴趣编写一个 PAM 模块 该模块将利用流行的 Unix 登录身份验证机制 我过去的大部分编程经验都是使用 Python 进行的 并且我正在交互的系统已经有一个 Python API 我用谷歌搜索发现pam python http pa
  • 如何生成给定范围内的回文数列表?

    假设范围是 1 X 120 这是我尝试过的 gt gt gt def isPalindrome s check if a number is a Palindrome s str s return s s 1 gt gt gt def ge
  • 如何在 Sublime Text 2 的 OSX 终端中显示构建结果

    我刚刚从 TextMate 切换到 Sublime Text 2 我非常喜欢它 让我困扰的一件事是默认的构建结果显示在 ST2 的底部 我的程序产生一些很长的结果 显示它的理想方式 如在 TM2 中 是并排查看它们 如何在 Mac 操作系统
  • Python 多处理示例不起作用

    我正在尝试学习如何使用multiprocessing但我无法让它发挥作用 这是代码文档 http docs python org 2 library multiprocessing html from multiprocessing imp
  • feedparser 在脚本运行期间失败,但无法在交互式 python 控制台中重现

    当我运行 eclipse 或在 iPython 中运行脚本时 它失败了 ascii codec can t decode byte 0xe2 in position 32 ordinal not in range 128 我不知道为什么 但
  • 表达式中的 Python 'in' 关键字与 for 循环中的比较 [重复]

    这个问题在这里已经有答案了 我明白什么是in运算符在此代码中执行的操作 some list 1 2 3 4 5 print 2 in some list 我也明白i将采用此代码中列表的每个值 for i in 1 2 3 4 5 print
  • Python - 在窗口最小化或隐藏时使用 pywinauto 控制窗口

    我正在尝试做的事情 我正在尝试使用 pywinauto 在 python 中创建一个脚本 以在后台自动安装 notepad 隐藏或最小化 notepad 只是一个示例 因为我将编辑它以与其他软件一起使用 Problem 问题是我想在安装程序
  • 检查所有值是否作为字典中的键存在

    我有一个值列表和一本字典 我想确保列表中的每个值都作为字典中的键存在 目前我正在使用两组来确定字典中是否存在任何值 unmapped set foo set bar keys 有没有更Pythonic的方法来测试这个 感觉有点像黑客 您的方
  • VSCode:调试配置中的 Python 路径无效

    对 Python 和 VSCode 以及 stackoverflow 非常陌生 直到最近 我已经使用了大约 3 个月 一切都很好 当尝试在调试器中运行任何基本的 Python 程序时 弹出窗口The Python path in your
  • 在 Pandas DataFrame Python 中添加新列[重复]

    这个问题在这里已经有答案了 例如 我在 Pandas 中有数据框 Col1 Col2 A 1 B 2 C 3 现在 如果我想再添加一个名为 Col3 的列 并且该值基于 Col2 式中 如果Col2 gt 1 则Col3为0 否则为1 所以
  • 如何使用google colab在jupyter笔记本中显示GIF?

    我正在使用 google colab 想嵌入一个 gif 有谁知道如何做到这一点 我正在使用下面的代码 它并没有在笔记本中为 gif 制作动画 我希望笔记本是交互式的 这样人们就可以看到代码的动画效果 而无需运行它 我发现很多方法在 Goo
  • 循环标记时出现“ValueError:无法识别的标记样式 -d”

    我正在尝试编码pyplot允许不同标记样式的绘图 这些图是循环生成的 标记是从列表中选取的 为了演示目的 我还提供了一个颜色列表 版本是Python 2 7 9 IPython 3 0 0 matplotlib 1 4 3 这是一个简单的代
  • 使用基于正则表达式的部分匹配来选择 Pandas 数据帧的子数据帧

    我有一个 Pandas 数据框 它有两列 一列 进程参数 列 包含字符串 另一列 值 列 包含相应的浮点值 我需要过滤出部分匹配列 过程参数 中的一组键的子数据帧 并提取与这些键匹配的数据帧的两列 df pd DataFrame Proce
  • 在 Python 类中动态定义实例字段

    我是 Python 新手 主要从事 Java 编程 我目前正在思考Python中的类是如何实例化的 我明白那个 init 就像Java中的构造函数 然而 有时 python 类没有 init 方法 在这种情况下我假设有一个默认构造函数 就像
  • 您可以在 Python 类型注释中指定方差吗?

    你能发现下面代码中的错误吗 米皮不能 from typing import Dict Any def add items d Dict str Any gt None d foo 5 d Dict str str add items d f
  • Spark.read 在 Databricks 中给出 KrbException

    我正在尝试从 databricks 笔记本连接到 SQL 数据库 以下是我的代码 jdbcDF spark read format com microsoft sqlserver jdbc spark option url jdbc sql
  • Pandas 与 Numpy 数据帧

    看这几行代码 df2 df copy df2 1 df 1 df 1 values 1 df2 ix 0 0 我们的教练说我们需要使用 values属性来访问底层的 numpy 数组 否则我们的代码将无法工作 我知道 pandas Data
  • PyAudio ErrNo 输入溢出 -9981

    我遇到了与用户相同的错误 Python 使用 Pyaudio 以 16000Hz 录制音频时出错 https stackoverflow com questions 12994981 python error audio recording

随机推荐

  • LR(1)分析法

    目录 1 LR 1 分析表和LR 1 文法 2 SLR冲突消解存在的问题 1 LR 1 和SLR 1 分析表构造方法的对比 2 SLR冲突消解存在的问题 3 LR K 项目 4 有效项目 5 构造LR 1 分析表的方法 6 例题分析 1 L
  • Python爱心程序(怦然心动)

    import random from math import sin cos pi log from tkinter import CANVAS WIDTH 640 画布的宽 CANVAS HEIGHT 640 画布的高 CANVAS CE
  • 硬件基础——数字电路门电路

    门电路与D触发器 一 与门 1 基本定义 与门又称 与电路 逻辑 积 逻辑 与 电路 是执行 与 运算的基本逻辑门电路 有多个输入端 一个输出端 当所有的输入同时为高电平 逻辑1 时 输出才为高电平 否则输出为低电平 逻辑0 2 真值表 3
  • 简述远程视频监控项目方案

    5G时代的到来和运营商不断的下调流量资费 使得远程视频监控系统更加的被广泛使用 视频监控中前端摄像机具有快速编码视频内容的能力视频图象数字化是实时编码压缩的 视频流被封装为编码成网络数字包 可以通过网络传输到后端的解码 存储设备 在局域网视
  • mysql float 1,MySql中float类型含义及参数详解

    float表示浮点数 通俗点来说的话 我们可以简单理解为小数 参数有两个 M表示精度 表示浮点数的位数 D表示标度 表示小数位数 M位数不包括小数点位数 举例 float 6 2 则最大范围表示 9999 99 9999 99 float所
  • 用python写一个解密JS混淆加密代码的代码。

    为了解密JS混淆加密代码 您可以使用以下Python代码 def deobfuscate obfuscated code 首先 使用JS解密器库 例如Javascript Deobfuscator 尝试解密代码 以下是使用Javascrip
  • VS2019最简单编译V8引擎方法

    文章目录 1 编译前的配置工作 1 1配置代理 1 2下载depot tools 1 3下载Windows SDK10 2 获取源码 2 1可能出现的错误 3 编译源码 3 1 VS2019编译 1 3 2 VS2019编译 2 3 3另外
  • Pandas-数据结构-DataFrame(七):添加元素、修改元素、删除元素

    一 添加元素 新增列 行并赋值 import numpy as np import pandas as pd df pd DataFrame np random rand 16 reshape 4 4 100 columns a b c d
  • List和Map使用Stream流的例子:

    1 遍历List并输出 List list Arrays asList apple banana orange list stream forEach System out println 2 过滤List中的元素 List list Ar
  • pytorch自定义loss损失函数

    自定义loss的方法有很多 但是在博主查资料的时候发现有挺多写法会有问题 靠谱一点的方法是把loss作为一个pytorch的模块 比如 class CustomLoss nn Module 注意继承 nn Module def init s
  • selenium-java的使用教程

    selenium的使用教程 概述 selenium 是一个用于Web应用程序测试的工具 Selenium测试直接运行在浏览器中 就像真正的用户在操作一样 支持的浏览器包括IE 7 8 9 10 11 Mozilla Firefox Safa
  • IntelliJ IDEA扩展_解决运行Command line is too long

    老版本解决方法 在项目文件夹 idea workspace xml中找到
  • 关于Anaconda中Jyputer Notebook启动后不自动跳转网页Juypter问题的解决

    首先Juypter启动页面 我之前打开后不启动 就手动复制粘贴http 后面的网址在浏览器打开 每次这样很麻烦 上网查了下 有所感悟 灵感来自这个博主 http t csdn cn NMPQs 彻底解决该问题 1 win r 启动cmd 2
  • (二十三)用几何布朗运动模拟股价走势

    几何布朗运动的定义与表达式 用几何布朗运动模拟未来股价 下面我们以华泰证券 601688 股票为例 根据其2016年 2019年的日收盘价数据得到收益率和波动率 模拟未来三年 2020年 2022年 的价格走势 模拟路径为100条 S0设定
  • 集成电路(芯片 ic chip)详解

    集成电路 英文的全称是Integrated Circuit 中文简称为IC 集成电路 也有称为蕊片或chip的 集成电路 就是将晶体管 电阻 电容 二极管等电子组件整合装至一芯片 chip 上所构成的元件 现在的大规模 集成电路 可以集成几
  • 使用Java复制某一路径下的所有sql文件到另一目录下

    package com zyx test import java io File import java io FileInputStream import java io FileOutputStream import java io I
  • springboot下将项目打包部署 完整版 亲测

    我是将项目 打包成jar的 完整的springboot项目打包部署过程 1 设置我们的端口号 application properties中 server port 8080 2 配置我们的pom文件
  • 基于卷积的图像分类识别(一):AlexNet

    本专栏介绍基于深度学习进行图像识别的经典和前沿模型 将持续更新 包括不仅限于 AlexNet ZFNet VGG GoogLeNet ResNet DenseNet SENet MobileNet ShuffleNet Eifficient
  • 对一个多维数组随机添加高斯噪音

    这是对上一篇对一个数组随机赋零的提升版 mu 0 sigma 0 12 for i in range 17 有17列数据 a date iloc i i 1 取出某一列 a np array a index np arange len a
  • CUnit例子

    2019独角兽企业重金招聘Python工程师标准 gt gt gt 关于CUnit的安装请自行百度 我的系统 fedora22 64bit 我的CUnit的头文件在 usr include CUnit 库文件在 usr lib64 文件 l