单元测试工具 CUnit 简介(2)
作者:网络转载 发布时间:[ 2013/8/30 14:28:53 ] 推荐标签:
6.3 框架错误的行为
当错误发生时,默认的行为是设置错误码并继续执行。断言失败并不认为是错误。错误包括测试包初始化或清理失败、显示地执行非活动状态的测试包或测试用例等。用户有可能倾向于当框架错误发生时停止测试,CUnit提供下面函数设置:
void CU_set_error_action(CU_ErrorAction action)
CU_ErrorAction CU_get_error_action(void)
错误行为码定义如下:
CUEA_IGNORE Run should be continued when an error condition occurs (default)
CUEA_FAIL Run should be stopped when an error condition occurs
CUEA_ABORT The application should exit() when an error conditions occurs
7. 示例
7.1 待测试模块代码
// module.h
#ifndef _MODULE_H_
#define _MODULE_H_
int add(int, int);
int sub(int, int);
#endif
// module.c
#include "module.h"
int add(int a, int b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
7.2 测试用例及其运行代码
// TestRun.c
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <CUnit/Console.h>
#include <CUnit/CUCurses.h>
#include "module.h"
void test_add1(void){
CU_ASSERT_EQUAL(add(1,2),3);
}
void test_add2(void){
CU_ASSERT_NOT_EQUAL(add(1,2),4);
}
void test_sub1(void){
CU_ASSERT_EQUAL(sub(1,2),-1);
}
void test_sub2(void){
CU_ASSERT_NOT_EQUAL(sub(1,2),0);
}
CU_TestInfo testcase1[] = {
{"test_for_add1:",test_add1},
{"test_for_add2:",test_add2},
CU_TEST_INFO_NULL
};
CU_TestInfo testcase2[] = {
{"test_for_sub1:",test_sub1},
{"test_for_sub2:",test_sub2},
CU_TEST_INFO_NULL
};
int suite_init(void){
return 0;
}
int suite_cleanup(void){
return 0;
}
CU_SuiteInfo suites[] = {
{"testSuite1",suite_init,suite_cleanup,testcase1},
{"testsuite2",suite_init,suite_cleanup,testcase2},
CU_SUITE_INFO_NULL
};
int main(int argc, char * argv[])
{
if(CU_initialize_registry()){
fprintf(stderr, " Initialization of Test Registry failed. ");
return CU_get_error();
}else{
if(CUE_SUCCESS != CU_register_suites(suites))
return CU_get_error();
//**** Automated Mode *****************
CU_set_output_filename("ModuleTest");
CU_list_tests_to_file();
CU_automated_run_tests();
//************************************/
/***** Basice Mode *******************
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
//************************************/
/*****Console Mode ********************
CU_console_run_tests();
//************************************/
/*****Curse Mode ********************
CU_curses_run_tests();
//*********************************/
CU_cleanup_registry();
return CU_get_error();
}
}
// Makefile
INC=-I/local/local/include
LIB=-L/local/local/lib
all: module.c TestRun.c
gcc $^ -o TestRun $(INC) $(LIB) -lcunit -lcurses -static
clean:
rm TestRun
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11