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:
- Write functions for tests (and suite init/cleanup if necessary).
- Initialize the test registry - CU_initialize_registry()
- Add suites to the test registry - CU_add_suite()
- Add tests to the suites - CU_add_test()
- Run tests using an appropriate interface, e.g. CU_console_run_tests
- 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_SUCCESS | suite creation was successful. |
CUE_NOREGISTRY | the registry has not been initialized. |
CUE_NO_SUITENAME | strName
was NULL . |
CUE_DUP_SUITE | the suite's name was not unique. |
CUE_NOMEMORY | memory allocation failed. |
CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc)
strName:一个test的名字
pTestFunc:函数指针
返回值:
CUE_SUCCESS | suite creation was successful. |
CUE_NOSUITE | the specified suite was NULL or invalid. |
CUE_NO_TESTNAME | strName
was NULL . |
CUE_NO_TEST | pTestFunc was NULL or invalid. |
CUE_DUP_TEST | the test's name was not unique. |
CUE_NOMEMORY | memory 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_SUCCESS | No error condition. |
CUE_NOMEMORY | Memory allocation failed. |
CUE_NOREGISTRY | Test registry not initialized. |
CUE_REGISTRY_EXISTS | Attempt to CU_set_registry() without CU_cleanup_registry(). |
CUE_NOSUITE | A required CU_pSuite pointer was NULL. |
CUE_NO_SUITENAME | Required CU_Suite name not provided. |
CUE_SINIT_FAILED | Suite initialization failed. |
CUE_SCLEAN_FAILED | Suite cleanup failed. |
CUE_DUP_SUITE | Duplicate suite name not allowed. |
CUE_NOTEST | A required CU_pTest pointer was NULL. |
CUE_NO_TESTNAME | Required CU_Test name not provided. |
CUE_DUP_TEST | Duplicate test case name not allowed. |
CUE_TEST_NOT_IN_SUITE | Test is not registered in the specified suite. |
CUE_FOPEN_FAILED | An error occurred opening a file. |
CUE_FCLOSE_FAILED | An error occurred closing a file. |
CUE_BAD_FILENAME | A bad filename was requested (NULL, empty, nonexistent, etc.). |
CUE_WRITE_ERROR | An error occurred during a write to a file. |
typedef enum CU_ErrorAction{
CUEA_IGNORE | Runs should be continued when an error condition occurs (default) |
CUEA_FAIL | Runs should be stopped when an error condition occurs |
CUEA_ABORT | The 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. |
==================================================================================