CUnit的精简测试框架
CUnit使用起来需要去写一些和CUnit相关的代码才能跑起来,没有JUnit4.0那种POJO的方式来的这么清爽,于是我定义了一些宏,将和业务无关的CUnit代码屏蔽起来,使用的时候,针对每个.C文件写一个测试suit文件,让它们一起包含Test.h即可。

Test.h出了包含一些头文件以外,还有一串宏,定义如下(下面的中文是举例):
- #include "include/Basic.h"
- #define INITIALIZE_CUNIT() \
- if (CUE_SUCCESS != CU_initialize_registry()) \
- return CU_get_error();
- #define FINISH_CUNIT() \
- CU_basic_set_mode(CU_BRM_VERBOSE); \
- CU_basic_run_tests(); \
- CU_cleanup_registry(); \
- return CU_get_error();
- #define NewTestSuit(SuitName) \
- CU_pSuite pSuite = NULL; \
- pSuite = CU_add_suite("Suite_Of_"#SuitName, before, after);
- #define AddTest(FunctionName) CU_add_test(pSuite, #FunctionName, FunctionName);
- #define GoTests() \
- test模块1(); \
- test模块2(); \
- test模块3();
- void test模块1(void);
- void test模块2(void);
- void test模块3(void);
下面是TestMain.c,以后可以不用动。
- #include "Test.h"
- int main(){
- INITIALIZE_CUNIT();
- GoTests();
- FINISH_CUNIT();
- }
下面给出 Test模块1.c 的例子
- #include "Test.h"
- static int before(void){
- // 该模块的方法运行前的准备工作写在这里
- return 0;
- }
- static int after(void){
- // 运行完的销毁工作也在这里
- return 0;
- }
- void test_方法1(void)
- {
- CU_ASSERT_EQUAL(1, 1);
- }
- void test_方法2(void)
- {
- CU_ASSERT_EQUAL(1, 1);
- }
- void test_方法3(void)
- {
- CU_ASSERT_EQUAL(1, 1);
- }
- void test模块1()
- {
- NewTestSuit(模块1);
- AddTest(test_方法1);
- AddTest(test_方法2);
- AddTest(test_方法3);
- }
需要注意的是,和JUnit4.0有点不同:
JUnit4.0运行流程为:before->方法1->after->before->方法2->after->before->方法3->after
CUnit的运行流程为: before->方法1->方法2->方法3->after