IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> C代码单元测试(三)_Google cmockery阅读 -> 正文阅读

[开发测试]C代码单元测试(三)_Google cmockery阅读

执行测试程序_Test Execution

Cmockery 单元测试程序使用void function(void **state)函数,使用unit_test*()宏定义函数初始化带有测试用例的函数指针的表

#define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }

然后将表传给runtest执行

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmockery.h>

// A test case that does nothing and succeeds.
void null_test_success(void **state) {
}

int main(int argc, char* argv[]) {
    const UnitTest tests[] = {
        unit_test(null_test_success),
    };
    return run_tests(tests);
}

异常处理_Exception Handling

在 run_tests() 执行测试函数之前,建立一个异常情况处理的程序,程序仅显示错误并在发生异常时退出测试函数。 如果在测试函数之外发生异常,例如在 Cmockery 本身中,应用程序将中止执行并返回错误代码。

错误条件_Failure Conditions

如果通过 run_tests() 执行测试函数期间发生故障,则测试函数将中止,应用程序的执行将继续执行下一个测试函数。测试失败通过 Cmockery 函数 fail() 发出信号。 以下事件将导致 Cmockery 库发布测试失败信号:

Assertions_断言测试

Cmockery中提供以下宏定义断言测试函数:

// Assert that the given expression is true.
assert_true(c)

// Assert that the given expression is false.
assert_false(c)

// Assert that the two given integers are equal, otherwise fail.
assert_int_equal(a, b) 

// Assert that the two given integers are not equal, otherwise fail.
assert_int_not_equal(a, b)

// Assert that the two given strings are equal, otherwise fail.
assert_string_equal(a, b)

// Assert that the two given strings are not equal, otherwise fail.
assert_string_not_equal(a, b)

// Assert that the two given areas of memory are equal, otherwise fail.
assert_memory_equal(a, b, size) 

// Assert that the two given areas of memory are not equal, otherwise fail.
assert_memory_not_equal(a, b, size) 

// Assert that the specified value is >= minimum and <= maximum.
assert_in_range(value, minimum, maximum) 

// Assert that the specified value is < minumum or > maximum
assert_not_in_range(value, minimum, maximum) 

// Assert that the specified value is within a set.
assert_in_set(value, values, number_of_values)

// Assert that the specified value is not within a set.
assert_not_in_set(value, values, number_of_values) 

比如我们assert_in_range断言宏来判断输入数据是否在要求范围之内,编写测试程序如下:

#include "all_head.h"

extern	int add_user(int a, int b);
extern	int sub_user(int a, int b);
/* Ensure add() adds two integers correctly. */
void test_add(void **state) {
	assert_int_equal(add_user(21, 1), 22);
	assert_int_equal(add_user(3, -3), 0);
}
/* Ensure sub() subtracts two integers correctly.*/
void test_sub(void **state) {
	assert_int_equal(sub_user(3, 3), 0);
	assert_int_equal(sub_user(3, -3), 6);
}

int main(int argc, char *argv[])
{
	const UnitTest tests[] = {
		unit_test(test_add),
		unit_test(test_sub),
	};
	return run_tests(tests);
}

被测函数代码如下:

#include "all_head.h"

//#define NDEBUG	1

#ifdef NDEBUG
#define assert_in_range(expression) ((void)0)
#else
#define assert_in_range(value, minimum, maximum) \
    _assert_in_range( \
        cast_to_largest_integral_type(value), \
        cast_to_largest_integral_type(minimum), \
        cast_to_largest_integral_type(maximum), __FILE__, __LINE__)

#endif

int add_user(int a, int b)
{
	assert_in_range(a, 1, 20);
	return a + b;
}
int sub_user(int a, int b)
{
	assert_in_range(a, 1, 20);
	return a - b;
}

在断言宏被调用的测试函数所在文件中,我们添加了一个宏定义NDEBUG和一个条件编译的部分。该部分的作用方便我们在使用过程中打开断言宏和关闭断言宏,在我们定义了NDEBUG时,断言宏虽然在程序中,但是并不会被调用。

打开断言宏,运行程序,结果如下:

test_add: Starting test

21 is not within the range 0-1
ERROR: g:\code_training\0811\c_test\c_test\testfunction.c:18 Failure!
test_add: Test failed.

test_sub: Starting test

test_sub: Test completed successfully.

1 out of 2 tests failed!
    test_add
请按任意键继续. . .

关闭断言宏的时候,运行代码,结果如下:

test_add: Starting test

test_add: Test completed successfully.

test_sub: Starting test

test_sub: Test completed successfully.

All 2 tests passed

请按任意键继续. . .

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-08-23 16:59:40  更:2021-08-23 17:00:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/15 3:31:15-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码