1 下載CUnit安裝包CUnit-2.1-3.tar.bz2保存至/home/用戶/
(安裝包版本為文章做成時2016-05-25的最新版本)
https://sourceforge.NET/projects/cunit
2 解壓縮安裝包
tar jxvf CUnit-2.1-3.tar.bz2
3 進入解壓后的文件夾CUnit-2.1-3
cd CUnit-2.1-3
4 執行make,分別執行以下命令
aclocal
autoheader
autoconf
automake
如果在automake過程中有文件丟失,則執行下面命令:
automake --add-missing
再次執行automake
如果出現錯誤:configure.in:161:required file './ltmain.sh' not found,則執行下面的命令:
libtoolize --automake --copy --debug --force
執行完上述步驟以后,查看是否生成了configure,如果有,則執行:
./configure
執行完上述步驟以后,查看是否生成了Makefile,如果有,則執行:
make
安裝編譯出的庫:
sudo make install
查看是否已經安裝成功:
ls /usr/local/lib/libcunit.so
5 樣例代碼做成
5.1 CUnit官網上的樣例代碼拷貝並保存至/home/用戶/test下面,記為test.c
http://cunit.sourceforge.net/example.html
5.2 編譯t.c
gcc -o t t.c -lcunit
5.3 運行t
./t
5.4正常運行的話會產生以下結果畫面
6 生成測試代碼覆蓋率報告
6.1 重新編譯t.c
gcc -fprofile-arcs -ftest-coverage -o t t.c -lcunit
6.2 運行t
./t
6.3 生成覆蓋率數據中間文件
gcov t.c
6.4 生成xml形式的覆蓋率報告文件至app.info文件
lcov --directory . --capture --output-file app.info
6.5 生成html形式的覆蓋率報告文件至results文件夾,數據來源文件為app.info
genhtml -o results app.info
6.6 正常的話會生成下述的html頁面