關鍵字:CUnit Linux 配置 xml
????? 由于項目需要,對于C語言的單元測試工具CUnit在Linux下如何使用進行了調查,在網上對相關內容進行搜索發現,很多內容都很相近,甚至完全一樣,在這篇爭相轉載的文章中,雖有詳細的說明,但也有描述的不甚清晰之處,對于剛剛接觸Linux的同學,往往是一頭霧水,不能很順利的配置出來。籍著此次的調查機會,現將具體的步驟和配置過程中需要注意的地方進行了補充說明,希望能對以后需要進行同樣工作的同學有些幫助。
1、首先在http://cunit.sourceforge.net/index.html鏈接處,下載最新版本的CUnit源碼包(CUnit-2.1-0-src.tar.gz)。
2、將CUnit源碼包(CUnit-2.1-0-src.tar.gz)復制到Linux的目標目錄下,比如我在這里放到了/usr/unittest目錄下。
3、CUnit源碼包的解壓。打開[System Tools]-〉[Terminal],進入到/usr/unittest目錄下,
輸入如下命令:
#tar xzvf CUnit-2.1-0-src.tar.gz
執行結束后,將會在當前目錄下生成一個解壓后的文件夾(CUnit-2.1-0)。
4、解壓結束后,開始進行編譯和安裝。
#cd CUnit-2.1-0
#aclocal
#autoconf
#automake
#chmod u+x configure
#./configure --prefix <Your choice of directory for installation>
(對上一句進行解釋,<Your choice of directory for installation>這個位置,需要你輸入要安裝的目錄,目錄的格式舉例如下:/usr/unittest/)
#make
#make install
這里需要一段時間...
#cd /usr/unittest/lib
#ldconfig
最后這兩句,感覺像是沒什么用,有時間證實一下。
到此處為止,CUnit的安裝基本上就到一段落了。
接下來是來測試我們的代碼工作流程。
將要測試的代碼復制到/usr/unittest目錄下,
輸入如下命令:
#export LD_LIBRARY_PATH=/usr/unittest/lib
#gcc -o test -I/usr/unittest/include -L/usr/unittest/lib -lcunit run_test.c test_func.c func.c
這樣,即可在/usr/unittest目錄下生成可執行文件test。
#./test
執行該文件,執行成功后,會在當前目錄下產生兩個xml文件。
①TestMax-Listing.xml :對測試用例的報告
②TestMax-Results.xml :對測試結果的報告
要查看這兩個文件,還需要使用如下xsl和dtd文件:
CUnit-List.dtd和CUnit-List.xsl用于解析列表文件,
CUnit-Run.dtd和CUnit-Run.xsl用于解析結果文件。
這四個文件在CUnit包里面有提供,安裝之后在unittest/share/CUnit目錄下,
默認安裝的話在/home/usr/local/share/CUnit目錄下。
在查看結果之前,需要把這六個文件:
TestMax-Listing.xml, TestMax-Results.xml, CUnit-List.dtd, CUnit-List.xsl, CUnit-Run.dtd, CUnit-Run.xsl拷貝到一個目錄下,然后用瀏覽器打開兩個結果的xml文件就可以了。




--------------------------------------------------
示例代碼如下:
func.c
--------
int maxi(int i, int j)
{
??? return i>j?i:j;
//??????? return i;
}
--------
test_func.c
--------
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "include/CUnit/CUnit.h"
#include "include/CUnit/Automated.h"
/**//*---- functions to be tested ------*/
extern int maxi(int i, int j);
/**//*---- test cases ------------------*/
void testIQJ()
{
??????? CU_ASSERT_EQUAL(maxi(1,1),1);
??????? CU_ASSERT_EQUAL(maxi(0,-0),0);
}
void testIGJ()
{
??????? CU_ASSERT_EQUAL(maxi(2,1),2);
??????? CU_ASSERT_EQUAL(maxi(0,-1),0);
??????? CU_ASSERT_EQUAL(maxi(-1,-2),-1);
}
void testILJ()
{
??????? 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; }
CU_SuiteInfo suites[] = {
??????? {"Testing the function maxi:", suite_success_init, suite_success_clean, testcases},
??????? CU_SUITE_INFO_NULL
};
/**//*---- setting enviroment -----------*/
void AddTests(void)
{
??????? assert(NULL != CU_get_registry());
??????? assert(!CU_is_test_running());
??????? /**//* shortcut regitry */
??????? if(CUE_SUCCESS != CU_register_suites(suites)){
??????????????? fprintf(stderr, "Register suites failed - %s ", CU_get_error_msg());
??????????????? exit(EXIT_FAILURE);
??????? }
}
--------
run_test.c
--------
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main( int argc, char *argv[] )
{
?????? printf("hello");
?????? if(CU_initialize_registry()){
??????????????? fprintf(stderr, " Initialization of Test Registry failed. ");
??????????????? exit(EXIT_FAILURE);
??????? }else{
??????????????? AddTests();
??????????????? CU_set_output_filename("TestMax");
??????????????? CU_list_tests_to_file();
??????????????? CU_automated_run_tests();
??????????????? CU_cleanup_registry();
??????? }
??????? return 0;
}
-----------------------end--------------------------------