CUnit 下载、安装、测试

本文档详细介绍了如何在Ubuntu 12.04上下载和安装CUnit 2-1-3,包括使用autoconf、automake进行编译,并解决了在配置过程中遇到的问题。同时,提到了CUnit的四种测试模式及其相关头文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Linux下autoconf和automake使用

http://www.cnblogs.com/itech/archive/2010/11/28/1890220.html


CUnit的首页:http://cunit.sourceforge.net/

CUnit 的文档手册地址:http://cunit.sourceforge.net/doc/index.html

下载CUnit 2-1-3地址:http://sourceforge.net/projects/cunit/?source=typ_redirect

CUnit 2-1-3 编译过程见README:

In addition to jam, the standard GNU build system is still supported.
  The usual sequence of steps should succeed in building and installing CUnit:
    1. aclocal  (if necessary)
    2. autoconf (if necessary)
    3. automake (if necessary)
    4. chmod u+x configure (if necessary)
    5. ./configure --prefix <Your choice of directory for installation>
    6. make
    7. make install

========================================================================================================

安装CUnit 2-1-3问题点:

问题一:configure.in:211: required file `config.h.in' not found

ANSWER:在执行automake --add-missing之前执行autoheader,生成config.h.in


问题二:required file `build/ltmain.sh' not found  
$libtoolize --version
-libtoolize (GNU libtool) 1.4.2
.....

$libtoolize --automake --copy --debug --force

=================================================================================

测试模式

下面是四种测试模式,使用如下的函数:
1 Automated Output to xml file            Non-interactive

      #include <CUnit/Automated.h>

  	void         CU_automated_run_tests(void)
  	CU_ErrorCode CU_list_tests_to_file(void)
  	void         CU_set_output_filename(const char* szFilenameRoot)

2 Basic      Flexible programming        interface Non-interactive  

        #include <CUnit/Basic.h>

  	 typedef enum    CU_BasicRunMode
 	 CU_ErrorCode    CU_basic_run_tests(void)
 	 CU_ErrorCode    CU_basic_run_suite(CU_pSuite pSuite)
 	 CU_ErrorCode    CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest)
  	 void            CU_basic_set_mode(CU_BasicRunMode mode)
  CU_BasicRunMode CU_basic_get_mode(void)
  void            CU_basic_show_failures(CU_pFailureRecord pFailure)

3 Console    Console interface (ansi C)     Interactive  

#include <CUnit/Console.h>

  	 void CU_console_run_tests(void)

4 Curses     Graphical interface (Unix)     Interactive

#include <CUnit/CUCurses.h>

  	void CU_curses_run_tests(void)
	注意点:需要连接 -lncurses
==========================================================================================
  			Test Registry
                            |
             ------------------------------
             |                            |
          Suite '1'      . . . .       Suite 'N'
             |                            |
       ---------------             ---------------
       |             |             |             |
    Test '11' ... Test '1M'     Test 'N1' ... Test 'NM'
======================================================================================================
基本流程
 General Usage
A typical sequence of steps for using the CUnit framework is:
 
  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
注意如下两个函数:
CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean);
strName:suite的名字
pInit:一个suite的初始化函数
pClean:一个suite的清理函数
返回值:
  
CUE_SUCCESSsuite creation was successful.
CUE_NOREGISTRYthe registry has not been initialized.
CUE_NO_SUITENAMEstrName was NULL.
CUE_DUP_SUITEthe suite's name was not unique.
CUE_NOMEMORYmemory allocation failed.

CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc)
strName:一个test的名字
pTestFunc:函数指针
返回值:

  
CUE_SUCCESSsuite creation was successful.
CUE_NOSUITEthe specified suite was NULL or invalid.
CUE_NO_TESTNAMEstrName was NULL.
CUE_NO_TESTpTestFunc was NULL or invalid.
CUE_DUP_TESTthe test's name was not unique.
CUE_NOMEMORYmemory allocation failed.
对于3、4步骤可以用如下函数:
CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]);/*注册CU_SuiteInfo数组 */

CU_ErrorCode CU_register_nsuites(int suite_count, ...);
typedef struct CU_TestInfo {
	char        *pName;     /**< Test name. */
	CU_TestFunc pTestFunc;  /**< Test function. */
} CU_TestInfo;
typedef CU_TestInfo* CU_pTestInfo;  /**< Pointer to CU_TestInfo type. */
typedef struct CU_SuiteInfo {
	char              *pName;        /**< Suite name. */
	CU_InitializeFunc pInitFunc;     /**< Suite initialization function. */
	CU_CleanupFunc    pCleanupFunc;  /**< Suite cleanup function */
	CU_SetUpfunc	  pSetUpFunc;    /*CUnit-2.2.3 增加的函数,Point to the test SetUp function
	CU_TearDownFunc	  pTearDownFunc; /*CUnit-2.2.3 增加的函数,Point to the test TearDown function
	/*pSetUpFunc :测试函数的开始被调用,
           pTearDownFunc:测试函数结束被调用
 */
	CU_TestInfo       *pTests;       /**< Test case array - must be NULL terminated. */
} CU_SuiteInfo;
typedef CU_SuiteInfo* CU_pSuiteInfo;  /**< Pointer to CU_SuiteInfo type. */

==================================================================================
错误处理:
#include <CUnit/CUError.h> (included automatically by <CUnit/CUnit.h>)
  typedef enum CU_ErrorCode
  CU_ErrorCode   CU_get_error(void);
  const char*    CU_get_error_msg(void);

  typedef enum CU_ErrorAction
  void           CU_set_error_action(CU_ErrorAction action);
  CU_ErrorAction CU_get_error_action(void);
typedef enum CU_ErrorCode{
    
CUE_SUCCESSNo error condition.
CUE_NOMEMORYMemory allocation failed.
CUE_NOREGISTRYTest registry not initialized.
CUE_REGISTRY_EXISTSAttempt to CU_set_registry() without CU_cleanup_registry().
CUE_NOSUITEA required CU_pSuite pointer was NULL.
CUE_NO_SUITENAMERequired CU_Suite name not provided.
CUE_SINIT_FAILEDSuite initialization failed.
CUE_SCLEAN_FAILEDSuite cleanup failed.
CUE_DUP_SUITEDuplicate suite name not allowed.
CUE_NOTESTA required CU_pTest pointer was NULL.
CUE_NO_TESTNAMERequired CU_Test name not provided.
CUE_DUP_TESTDuplicate test case name not allowed.
CUE_TEST_NOT_IN_SUITETest is not registered in the specified suite.
CUE_FOPEN_FAILEDAn error occurred opening a file.
CUE_FCLOSE_FAILEDAn error occurred closing a file.
CUE_BAD_FILENAMEA bad filename was requested (NULL, empty, nonexistent, etc.).
CUE_WRITE_ERRORAn error occurred during a write to a file.
};
  typedef enum CU_ErrorAction{
     
CUEA_IGNORERuns should be continued when an error condition occurs (default)
CUEA_FAILRuns should be stopped when an error condition occurs
CUEA_ABORTThe application should exit() when an error conditions occurs
};
=============================================================================================
断言函数:
注意:They may also use FATAL versions of assertions - failure will abort the original test function and its entire call chain.

#include <CUnit/CUnit.h>

CU_ASSERT(int expression) CU_ASSERT_FATAL(int expression) CU_TEST(int expression) CU_TEST_FATAL(int expression)Assert that expression is TRUE (non-zero)
CU_ASSERT_TRUE(value) CU_ASSERT_TRUE_FATAL(value)Assert that value is TRUE (non-zero)
CU_ASSERT_FALSE(value) CU_ASSERT_FALSE_FATAL(value)Assert that value is FALSE (zero)
CU_ASSERT_EQUAL(actual, expected) CU_ASSERT_EQUAL_FATAL(actual, expected)Assert that actual = = expected
CU_ASSERT_NOT_EQUAL(actual, expected)) CU_ASSERT_NOT_EQUAL_FATAL(actual, expected)Assert that actual != expected
CU_ASSERT_PTR_EQUAL(actual, expected) CU_ASSERT_PTR_EQUAL_FATAL(actual, expected)Assert that pointers actual = = expected
CU_ASSERT_PTR_NOT_EQUAL(actual, expected) CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected)Assert that pointers actual != expected
CU_ASSERT_PTR_NULL(value) CU_ASSERT_PTR_NULL_FATAL(value)Assert that pointer value == NULL
CU_ASSERT_PTR_NOT_NULL(value) CU_ASSERT_PTR_NOT_NULL_FATAL(value)Assert that pointer value != NULL
CU_ASSERT_STRING_EQUAL(actual, expected) CU_ASSERT_STRING_EQUAL_FATAL(actual, expected)Assert that strings actual and expected are equivalent
CU_ASSERT_STRING_NOT_EQUAL(actual, expected) CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected)Assert that strings actual and expected differ
CU_ASSERT_NSTRING_EQUAL(actual, expected, count) CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count)Assert that 1st count chars of actual and expected are the same
CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count)Assert that 1st count chars of actual and expected differ
CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity)Assert that |actual - expected| <= |granularity| Math library must be linked in for this assertion.
CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity)Assert that |actual - expected| > |granularity| Math library must be linked in for this assertion.
CU_PASS(message)Register a passing assertion with the specified message. No logical test is performed.
CU_FAIL(message) CU_FAIL_FATAL(message)Register a failed assertion with the specified message. No logical test is performed.
==================================================================================

 
 

 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值