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