單元測試(Unittest)一直是軟體開發的好幫手,但場景切換到 C 語言原始碼的 Legacy Code,似乎處處碰壁。本系列文章將帶你入門如何使用 Google Test 的 C++ 框架來實現對舊有的 C Code 做 Unittest,搭配 VS Code 的 Debugger,幫助你持續穩定程式品質。
$ cmake -S . -B build -G "MSYS Makefiles"
-- The C compiler identification is GNU 12.2.0
-- The CXX compiler identification is GNU 12.2.0
-- Found Python: C:/Users/zhung/AppData/Local/Programs/Python/Python310/python.exe (found version "3.10.11") found components: Interpreter
-- Configuring done (80.2s)
-- Generating done (0.7s)
-- Build files have been written to: D:/your/path/to/build
留意,我們特別指定使用 MSYS Makefiles,以避免 CMake 優先使用 Visual Studio 20XX with Windows SDK 環境。
輸入以下指令開始編譯,現在我們的 CMakeLists.txt 設定只有帶入 gtest 主程式。
$ cmake --build build
[ 12%] Building CXX object googletest/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj
[ 37%] Building CXX object googletest/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.obj
[ 62%] Building CXX object googletest/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.obj
[ 87%] Building CXX object googletest/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.obj
[100%] Linking CXX static library ../../lib/libgtest_main.a
[100%] Built target gtest_main
可以加入 –verbose 參數以查看詳細編譯指令。
沒問題的話,就表示你的系統可以正常編譯 Google Test 主程式啦!
撰寫第一則測試
現在,在根目錄下加入一個 test 資料夾,我們要把 Test 檔案集中在這邊,例如加入一支 test_list8.cc。
$ cmake --build build
[100%] Built target gmock_main
加入參數 -j 4 可以使用4個執行緒以加速。
沒問題的話,輸入以下指令執行測試:
$ ctest --test-dir build
Internal ctest changing into directory: D:/Work/Desktop/googletest/build
Test project D:/Work/Desktop/googletest/build
Start 1: TestList8.CaseInit
1/1 Test #1: TestList8.CaseInit ............... Passed 0.35 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 0.38 sec
Start testing: Apr 24 11:55
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestList8
[ RUN ] TestList8.CaseInit
[ OK ] TestList8.CaseInit (0 ms)
[----------] 1 test from TestList8 (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 1 test.
Test time = 0.35 sec
----------------------------------------------------------
Test Passed.
在 VS Code 上使用 Debugger
如果你會使用 gdb 直接 Debug,可以跳過這段。若還是想用 VS Code 的界面,請安裝套件 CMake Tools,重新用 VS Code 打開你的專案目錄,就可以發現狀態欄多出許多按鈕啦!加入 Debug 用的啟動設定於 .vscode/launch.json。
"version": "0.2.0",
"configurations": [
// source: https://github.com/microsoft/vscode-cmake-tools/blob/main/docs/debug-launch.md
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
// Resolved by CMake Tools:
"program": "${command:cmake.launchTargetPath}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
// add the directory where our target was built to the PATHs
// it gets resolved by CMake Tools:
"name": "PATH",
"value": "${env:PATH}:${command:cmake.getLaunchTargetDirectory}"
"name": "OTHER_VALUE",
"value": "Something something"
"console": "externalTerminal",
"MIMode": "gdb",
"setupCommands": [
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
然後在 Debugger Panel 中點選綠色箭頭 ▶,就可以 Debug 啦!嗯?好像哪裏怪怪的。你可能會發現只有 google test (C++) 的程式部分可以跳轉,C 程式都進不去,原因是 gcc 預設 Release Build,不會有 Debug 用的檔案資訊。回到我們 CMakeLists.txt,加入以下設定: