# Install
# Windows
下載並安裝 MinGW https://sourceforge.net/projects/mingw/
安裝下圖出現的套件
添加環境變數如下
下載 CUnit-2.1-3 https://sourceforge.net/projects/cunit/
複製 CUnit-2.1-3 到 (C:\MinGW\msys\1.0\home) ,並解壓縮
啟動 MSYS,預設路徑 (C:\MinGW\msys\1.0\msys.bat),並執行以下命令
cd /home/CUnit-2.1-3 | |
./boostrap | |
make | |
make install |
複製 C:\MinGW\msys\1.0\home\User\CUnitHome\lib 底下的 libcunit.a 到 C:\MinGW\lib
複製 C:\MinGW\msys\1.0\home\User\CUnitHome\include 底下的 CUnit 到 C:\MinGW\include
# Linux
更新 apt 資料庫
sudo apt update |
安裝以下套件
sudo apt install gcc g++ lcov | |
sudo apt install libcunit1 libcunit1-dev libcunit1-doc libcunit1-ncurses libcunit1-ncurses-dev |
# Testing
# Example
#include <stdio.h> | |
#include <stdbool.h> | |
#include <CUnit/CUnit.h> | |
#include <CUnit/Basic.h> | |
#include <CUnit/TestRun.h> | |
#include <CUnit/Automated.h> | |
int iCompute(int iA, int iB, char cCommand[]) { | |
int i; | |
for (i = 0 ; i < strlen(cCommand) ; i++) { | |
if (cCommand[i] == 'A') | |
iA = iA + iB; | |
else if (cCommand[i] == 'B') | |
iB = iA + iB; | |
else | |
return -1; | |
} | |
return iA + iB; | |
} | |
int ans[2] = {25,-1}; | |
void test(void) { | |
CU_ASSERT(iCompute(5,2,"ABA") == ans[0]); | |
CU_ASSERT(iCompute(5,2,"CCC") == ans[1]); | |
} | |
int main(void) { | |
CU_initialize_registry(); | |
CU_pSuite suite = CU_add_suite("test", NULL, NULL); | |
CU_add_test(suite, "test", test); | |
CU_basic_set_mode(CU_BRM_VERBOSE); | |
CU_basic_run_tests(); | |
CU_cleanup_registry(); | |
return 0; | |
} |
# Windows
// --coverage - The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking) | |
gcc -c main.c -o main.o --coverage | |
gcc main.o -o main -lgcov -lcunit | |
main > result.txt |
# Linux
// --coverage - The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking) | |
gcc -c main.c -o main.o --coverage | |
gcc main.o -o main -lgcov -lcunit | |
./main > result.txt |
# Coverage Testing
# Windows
gcov main.gcda | |
ren main.c.gcov coverage.txt |
# Linux
gcov main.gcda | |
mv main.c.gcov coverage.txt |
產生 HTML 圖表
lcov -c -d . -o test.info --rc lcov_branch_coverage=1 | |
genhtml --branch-coverage -o result test.info |
# 參考來源
- https://blog.csdn.net/godmaycry/article/details/77480549
- https://blog.csdn.net/qq_44610809/article/details/122364659
- https://zhuanlan.zhihu.com/p/410077415
- https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#Instrumentation-Options