单元测试工具 CUnit 简介(2)

发表于:2015-01-09来源:uml.org.cn作者:不详点击数: 标签:单元测试工具
所有的断言被定义在 3. 测试注册簿 3.1 常用相关函数 #include CUnit/TestDB.htypedef struct CU_TestRegistrytypedef CU_TestRegistry* CU_pTestRegistryCU_ErrorCode CU_initialize_registry(

  所有的断言被定义在

  3. 测试注册簿

  3.1 常用相关函数

#include  <CUnit/TestDB.h>
typedef struct CU_TestRegistry
typedef CU_TestRegistry* CU_pTestRegistry
CU_ErrorCode CU_initialize_registry(void)
void CU_cleanup_registry(void)
CU_BOOL CU_registry_initialized(void)
CU_pTestRegistry CU_get_registry(void)
CU_pTestRegistry CU_set_registry(CU_pTestRegistry pTestRegistry)
CU_pTestRegistry CU_create_new_registry(void)
void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry)

  3.2 注册簿内部结构体

  测试注册簿是测试包和相关测试用例的仓库。当用户添加测试包或测试用例时,CUnit维护当前活动的测试注册簿的状态更新,当用户选择运行所有测试用例时,当前活动的注册簿中所有的测试包均被执行。

  测试注册簿结构在中定义,它包括所有测试包的数量、所有测试用例的数量以及一个指向该注册簿中测试包链表的指针:

typedef struct CU_TestRegistry
{
unsigned int uiNumberOfSuites;
unsigned int uiNumberOfTests;
CU_pSuite    pSuite;
} CU_TestRegistry;
typedef CU_TestRegistry* CU_pTestRegistry;

  3.3 与注册簿相关的其它函数

CU_pTestRegistry CU_get_registry(void)
CU_pTestRegistry CU_set_registry(CU_pTestRegistry pTestRegistry)
CU_pTestRegistry CU_create_new_registry(void)
void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry)

  4. 测试包及测试用例的管理

  4.1 相关函数及结构

#include <CUnit/TestDB.h>
typedef struct CU_Suite
typedef CU_Suite* CU_pSuite
typedef struct CU_Test
typedef CU_Test* CU_pTest
typedef void (*CU_TestFunc)(void)
typedef int (*CU_InitializeFunc)(void)
typedef int (*CU_CleanupFunc)(void)
CU_pSuite CU_add_suite(const char* strName,CU_InitializeFunc pInit,CU_CleanupFunc pClean);
CU_pTest   CU_add_test(CU_pSuite pSuite,const char* strName,CU_TestFunc pTestFunc);
typedef struct CU_TestInfo
typedef struct CU_SuiteInfo
CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]);
CU_ErrorCode CU_register_nsuites(int suite_count, ...);
CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive)
CU_ErrorCode CU_set_test_active(CU_pTest, CU_BOOL fNewActive)
CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName)
CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit)
CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean)
CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName)
CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc)
CU_pSuite CU_get_suite(const char* strName)
CU_pSuite CU_get_suite_at_pos(unsigned int pos)
unsigned int CU_get_suite_pos(CU_pSuite pSuite)
unsigned int CU_get_suite_pos_by_name(const char* strName)
CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName)
CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos)
unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest)
unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName)

  4.2 注册测试包

CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc    pInit, CU_CleanupFunc pClean)

  创建一个测试包,该测试包拥有自己特定的名字、初始化函数及清理函数。该测试包被注册到一个测试注册簿,该注册簿在添加任意测试包之前须初始化。当前版本不支持独立于注册簿之外的测试包的创建,该函数不应该在测试执行期间被调用。

  在注册簿中,推荐每个测试包有唯一的名字,这样可以通过名字查找测试包。在上述函数中,测试包的初始化函数和清理函数是可选的,如果不需要这些函数可以传参数NULL。

原文转自:http://www.uml.org.cn/Test/201404151.asp