Windows下如何搭建CUNIT环境资料很多,但是错误不少或者讲解不清晰,很容易让人跌入坑中,现在介绍如下。
1 安装mingw
网上多处文章介绍下载mingw-get,由于现在网上大都数mingw-get均为32位的,所以不能用这个。所以大家需要下载https://sourceforge.net/projects/mingw-w64/files/下载mingw-w64-install.exe,在安装的时候注意:
i686为32位的,x86_64为64位的。所以这里我们选择x86_64。安装完毕配置环境变量。加入MINGW_HOME环境变量,我这里路径为:C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\。在PATH中设置%MinGW_HOME%\mingw64\bin\和%MinGW_HOME%\mingw64\include\。打开cmd,输入gcc,如果返回gcc: fatal error: no inputfiles则说明配置正确。
2 下载JUNIT tar包
JUNIT tar包是为Linux开发的,但是在Windows下可用msys2工具进行编译。我解压完毕放在C:\CUnit-2.1-3目录下
3 安装msys2
msys2可以让你在Windows下编译Linux的代码,目前网站上有简易版和完全版,简易版下载以后还需要安装各个命令的插件,比较麻烦,所以建议安装完全版,完整版的文件名为msys+7za+wget+svn+git+mercurial+cvs-rev13.7z(这个文件比较大,下载需要1个多小时)。解压完毕请把文件夹msys全部拷贝到mingw的目录下,我这里为C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64。
进入C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys,打开msys.bat。执行以下命令:
cd C:\CUnit-2.1-3 #解压的CUnit的根目录 libtoolize automake --add-missing autoreconf ./configure --prefix=/mingw make make install
安装完毕把C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys\mingw\lib\libcunit.a拷贝到C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib。(不拷贝在下面gcc或者clang运行中,加入-lcunit参数会提示..lib: can’t find -lcunit的错误)
接下来把C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\msys\mingw\include\CUnit\目录中的所有.h文件拷贝到C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\include中去。
4 建立测试文件和被测文件
在任意工程目录下建立被测试文件:process.h和process.c
process.h:
extern int process(int x, int y, int z);
process.c:
#include <stdio.h> int process(int x, int y, int z){ int k=0; int j=0; if ((x>3) && (z<10)){ k=x*y-1; j=k^2; } if((x==4) || (y>5)){ j=x*j+10; } j=j%3; return k+j; }
和测试文件test_main.c
#include"process.h" /* test cases */ voidtest_process_1(void) { CU_ASSERT(process(4,6,9)==24); } voidtest_process_2(void) { CU_ASSERT(process(4,5,10)==1); } voidtest_process_3(void) { CU_ASSERT(process(5,4,9)==21); } voidtest_process_4(void) { CU_ASSERT(process(2,5,10)==0); } CU_TestInfotests[] = { {"test 1", test_process_1 }, {"test 2", test_process_2 }, {"test 3", test_process_3 }, {"test 4", test_process_4 }, CU_TEST_INFO_NULL }; /* suite init */ intsuite_init(void) { return 0; } intsuite_clean(void) { return 0; } voidsuite_setup(void) { } voidsuite_teardown(void) { } CU_SuiteInfosuites[] = { {"suite 1", suite_init,suite_clean, suite_setup, suite_teardown, tests}, CU_SUITE_INFO_NULL }; /* registry */ int main(intargc, char* argv[]) { CU_ErrorCode err; /* init */ printf("init\n"); err = CU_initialize_registry(); if( err ){ printf("CU_initialize_registry:%d\n", err); return err; } /* add suites and tests */ printf("add suites andtests\n"); err = CU_register_suites(suites); if( err ) { printf("CU_register_suites:%d\n", err); } CU_pTestRegistry reg = CU_get_registry(); printf("CU_get_registry:%d/%d/%u\n", reg->uiNumberOfSuites, reg->uiNumberOfTests,(long)reg->pSuite); /* run auto */ printf("run auto\n"); /**** Automated Mode *****************/ CU_set_output_filename("TestProcess"); CU_list_tests_to_file(); CU_automated_run_tests(); //************************************/ printf("run basic\n"); /***** Basice Mode *******************/ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); //************************************/ /*****Console Mode ******************** CU_console_run_tests(); //************************************/ /* end */ printf("end\n"); CU_cleanup_registry(); err = CU_get_error(); if( err ) { printf("error: %d",err); } return err; }
通过以下命令编译(可以把这个命令放入一个bat文件中):
gcc process.c test_main.c -o test-I/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/include-L/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/lib -lcunit -static
编译完成出现一个名为test.exe的文件,在cmd中运行它,会产生测试结果
C:\MyC\process>test init add suites andtests CU_get_registry:1/4/1909760 run auto run basic CUnit - A unit testing framework for C -Version 2.1-3 http://cunit.sourceforge.net/ Suite: suite 1 Test: test 1 ...passed Test: test 2 ...passed Test: test 3 ...passed Test: test 4 ...passed RunSummary: Type Total Ran Passed Failed Inactive suites 1 1 n/a 0 0 tests 4 4 4 0 0 asserts 4 4 4 0 n/a Elapsed time= 0.048 seconds end