Android系統中使用Cunit測試C/C++接口
Cunit是C/C++語言的單元測試框架,但常用于Windows和Linux開發中。
Android系統中經常有jni、so庫、hal service等都是C/C++實現,本文講解如何將Cunit嵌入Android中,用于測試一些C/C++ api。
Cunit簡介
Cunit是很早的C/C++接口測試框架,官網如下:
https://cunit.sourceforge.net/contact.html
測試模式有4種:
模式 | 介紹 |
---|---|
Basic | 最常用的,結果輸出到標準輸出(stdout) |
Automated | 生成完XML文件之后,然后再將CUnit-List.dtd、CUnit-List.xsl、CUnit-Run.dtd、CUnit-Run.xsl(這幾個文件在CUnit的源碼包可以找到)和XML文件放到同一級目錄,再用IE瀏覽器打開,就可以看到漂亮的界面了。 |
Console | 比較靈活,可以選擇只執行其中某一個測試用例。 |
Curses | 跟Console類似,只不過是以Curses窗口的方式展示。 |
模式 | 平臺 | 結果輸出方式 | 使用的接口函數 |
---|---|---|---|
Basic | 所有 | 標準輸出 | #include “CUnit/Basic.h” CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); |
Automated | 所有 | xml文件 | #include “CUnit/Automated.h” CU_list_tests_to_file(); CU_automated_run_tests(); |
Console | 所有 | 交互式控制臺 | #include “CUnit/Console.h” CU_console_run_tests(); |
Curses | Linux/Unix | 交互式curses窗口 | #include “CUnit/CUCurses.h” CU_curses_run_tests(); |
這4種模式最終的測試效果如下:
https://cunit.sourceforge.net/screenshots.html
模式 | 測試結果呈現 |
---|---|
Basic | https://cunit.sourceforge.net/ss_basic.html |
Automated | https://cunit.sourceforge.net/ss_automated.html |
Console | https://cunit.sourceforge.net/ss_console.html |
Curses | https://cunit.sourceforge.net/ss_curses.html |
具體的使用文檔可以參考如下:
https://cunit.sourceforge.net/documentation.html
https://cunit.sourceforge.net/doc/index.html
中文文檔:
https://blog.csdn.net/iuices/article/details/115280751
測試demo:
https://cunit.sourceforge.net/example.html
源碼下載:
https://sourceforge.net/projects/cunit/
交流論壇:
https://sourceforge.net/p/cunit/discussion/
在Android中使用
編譯
libcunit_android庫的編譯文件Android.bp:
cc_library_shared {name: "libcunit_android",local_include_dirs: ["CUnit/Headers","CUnit/Sources/Test",],srcs: ["CUnit/Sources/Framework/TestRun.c","CUnit/Sources/Framework/TestDB.c","CUnit/Sources/Framework/Util.c","CUnit/Sources/Framework/CUError.c","CUnit/Sources/Framework/MyMem.c","CUnit/Sources/Console/Console.c","CUnit/Sources/Basic/Basic.c","CUnit/Sources/Test/test_cunit.c","CUnit/Sources/Automated/Automated.c",],cflags: ["-DMEMTRACE","-DCUNIT_BUILD_TESTS","-DCUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS",],host_ldlibs: ["-llog"],
}cc_binary {name: "test_cunit_android",local_include_dirs: ["CUnit/Headers","CUnit/Sources/Test",],srcs: ["CUnit/Sources/Test/test_cunit.c"],cflags: ["-DCUNIT_BUILD_TESTS","-DCUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS",],shared_libs: ["libcunit_android"],}
libcunit_android_test的編譯Android.bp
cc_binary {name: "libcunit_android_test",include_dirs: [".","./libcunit_android/CUnit/Headers/",],srcs: ["main.c","test.c","testcase.c",],shared_libs: ["libcunit_android"],}
如何調用框架
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Basic.h"
#include "Automated.h"extern void AddTests(void);int main(int argc, char* argv[])
{CU_BasicRunMode mode = CU_BRM_VERBOSE;CU_ErrorAction error_action = CUEA_IGNORE;int i;//標準庫輸出 無緩沖:不使用緩沖。每個 I/O 操作都被即時寫入。buffer 和 size 參數被忽略。setvbuf(stdout, NULL, _IONBF, 0);for (i=1 ; i<argc ; i++) {if (!strcmp("-i", *argv)) {//錯誤發生時繼續執行(默認)error_action = CUEA_IGNORE;}else if (!strcmp("-f", *argv)) {//錯誤發生時應系統停止error_action = CUEA_FAIL;}else if (!strcmp("-A", *argv)) {//錯誤發生時系統應退出(EXIT)error_action = CUEA_ABORT;}else if (!strcmp("-s", *argv)) {//只會輸出錯誤信息mode = CU_BRM_SILENT;}else if (!strcmp("-n", *argv)) {//結果會輸出基本信息,包括失敗以及測試運行的總體狀況mode = CU_BRM_NORMAL;}else if (!strcmp("-v", *argv)) {//輸出測試運行的詳細信息mode = CU_BRM_VERBOSE;}else if (!strcmp("-e",*argv)) {return 0;}else {printf("\nUsage:BasicTest [options]\n\n""Options:-i ignore framework errors [default].\n"" -f fail on framework error.\n"" -A abort on framework error.\n\n"" -s silent mode - no output to screen.\n"" -n normal mode - standard output to screen.\n"" -v verbose mode - max output to screen [default].\n\n"" -e print expected test results and exit.\n"" -h print this message and exit.\n\n");return 0;}
}//CU_initialize_registry registry初始化//用戶在調用任何其他CUnit函數之前調用本函數,如果不這樣做可能會導致系統崩潰。
if (CU_initialize_registry()) {printf("\nInitialization of Test Registry failed.");
}else {AddTests();//CU_basic_set_mode()設置運行模式CU_basic_set_mode(mode);//CU_set_error_action設置錯誤發生時,系統的行為CU_set_error_action(error_action);//CU_basic_run_tests 運行Tests Basic Mode 基本擴展編程方式 非交互式printf("\nTests completed with return value %d.\n",CU_basic_run_tests());//使用console控制交互界面的函數入口 //CU_console_run_tests();/***使用自動產生XML文件的模式********/CU_set_output_filename("E:\\xml_test\\main");CU_list_tests_to_file();CU_automated_run_tests();/***********************************/// registry釋放CU_cleanup_registry();}return 0;
}
testcase.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "CUnit.h"
#include <Automated.h>
#include <TestDB.h>
/**//*---- functions to be tested ------*/
extern int maxi(int i , int j);/**//*---- test cases ------------------*/
void testIQJ(void)
{//斷言相等比較CU_ASSERT_EQUAL(maxi(1,1),1);CU_ASSERT_EQUAL(maxi(0,-0),0);
}
void testIGJ(void)
{CU_ASSERT_EQUAL(maxi(2,1),2);CU_ASSERT_EQUAL(maxi(0,-1),0);CU_ASSERT_EQUAL(maxi(-1,-2),-1);
}
void testILJ(void)
{CU_ASSERT_EQUAL(maxi(1,2),2);CU_ASSERT_EQUAL(maxi(-1,0),0);CU_ASSERT_EQUAL(maxi(-2,-1),-1);
}
CU_TestInfo testcases[] = {{"Testing i equals j:",testIQJ},{"Testing i greater than j:",testIGJ},{"Testing i less than j:", testILJ},CU_TEST_INFO_NULL
};
/**//*---- test suites ------------------*/
int suite_success_init(void)
{ return 0; }
int suite_success_clean(void)
{ return 0; }//需要運行的test case
CU_SuiteInfo suites[] = {{"Testing the function maxi:",suite_success_init,suite_success_clean, NULL, NULL,testcases},CU_SUITE_INFO_NULL
};
/*cunit運行環境設置*/
void AddTests(void)
{//1.CU_get_registry CU_register_suites其他一些關于注冊的內部函數,主要用于內部和測試的目的assert(NULL != CU_get_registry());assert(!CU_is_test_running());//注冊suitesif(CUE_SUCCESS != CU_register_suites(suites)){fprintf(stderr, "Register suites failed - %s ", CU_get_error_msg());exit(EXIT_FAILURE);}
}
test.c
/**
*file:test.c
**/
int maxi(int i,int j)
{return i>j?i:j;
}
修改Cunit框架
當前Result列表如下:
當前List列表如下:
新增一個需求:
需要在測試結果的列表中,添加一列專門用于記錄測試的值。
比如,需要記錄每個api接口的property或者value值。
【四步走】
- 首先,添加一個成員到類中:
-
在解析屬性的時候,記錄到xml文件中:
-
在對應的xml解析文件中修改為如下:
-
在每個對應的test case中,將對應的字符串保存到結構體:
最終的效果如下:
源碼下載
https://github.com/KingofHubGit/CTest