GoogleTest 使用指南 | 自动化测试运行
GoogleTest 使用指南 | 自动化测试运行
自动化测试运行能够大幅提升测试效率,减少手动操作,提高测试的频率和一致性。以下介绍几种常见的自动化测试策略。
本地自动化脚本
编写脚本自动化执行测试过程,包括构建、运行测试和生成报告。
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
BUILD_DIR="${BUILD_DIR:-build-coverage}"
REPORT_DIR="${BUILD_DIR}/coverage-report"
CTEST_REPORT="${BUILD_DIR}/ctest-report.xml"
COVERAGE_INFO="${BUILD_DIR}/coverage.info"
COVERAGE_FILTERED_INFO="${BUILD_DIR}/coverage.filtered.info"
CLEAN_BUILD=0
OPEN_REPORT=0
usage() {
cat <<EOF
Usage: $0 [--clean] [--open]
Options:
--clean Remove ${BUILD_DIR} before configuring.
--open Open the generated HTML coverage report after completion.
Environment:
BUILD_DIR Override build directory. Default: build-coverage
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--clean)
CLEAN_BUILD=1
shift
;;
--open)
OPEN_REPORT=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "Missing required command: $1" >&2
echo "Install dependencies with: brew install gcc lcov cmake" >&2
exit 1
fi
}
find_latest_gcc_version() {
local brew_prefix
brew_prefix="$(brew --prefix)"
find "${brew_prefix}/bin" -maxdepth 1 \( -type f -o -type l \) -name 'gcc-[0-9]*' \
| sed 's/.*gcc-//' \
| sort -n \
| tail -1
}
require_command brew
require_command cmake
require_command ctest
require_command lcov
require_command genhtml
BREW_PREFIX="$(brew --prefix)"
GCC_VERSION="${GCC_VERSION:-$(find_latest_gcc_version)}"
if [[ -z "${GCC_VERSION}" ]]; then
echo "No Homebrew GCC found. Install it with: brew install gcc" >&2
exit 1
fi
CC="${BREW_PREFIX}/bin/gcc-${GCC_VERSION}"
CXX="${BREW_PREFIX}/bin/g++-${GCC_VERSION}"
GCOV="${BREW_PREFIX}/bin/gcov-${GCC_VERSION}"
for tool in "${CC}" "${CXX}" "${GCOV}"; do
if [[ ! -x "${tool}" ]]; then
echo "Expected tool is not executable: ${tool}" >&2
exit 1
fi
done
cd "${REPO_ROOT}"
if [[ "${CLEAN_BUILD}" -eq 1 ]]; then
echo "Cleaning ${BUILD_DIR}"
cmake -E rm -rf "${BUILD_DIR}"
fi
echo "Using GCC ${GCC_VERSION}"
echo "Configuring ${BUILD_DIR}"
cmake -S . -B "${BUILD_DIR}" \
-DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_C_COMPILER="${CC}" \
-DCMAKE_CXX_COMPILER="${CXX}" \
-DCMAKE_C_FLAGS="--coverage -O0 -g" \
-DCMAKE_CXX_FLAGS="--coverage -O0 -g" \
-DCMAKE_EXE_LINKER_FLAGS="--coverage"
echo "Building"
cmake --build "${BUILD_DIR}"
echo "Resetting coverage counters"
lcov --directory "${BUILD_DIR}" --zerocounters >/dev/null
echo "Running tests"
ctest --test-dir "${BUILD_DIR}" \
--output-on-failure \
--output-junit "${CTEST_REPORT}"
echo "Capturing coverage"
lcov --capture \
--directory "${BUILD_DIR}" \
--gcov-tool "${GCOV}" \
--ignore-errors inconsistent \
--output-file "${COVERAGE_INFO}"
echo "Filtering coverage"
lcov --remove "${COVERAGE_INFO}" \
"*/_deps/*" \
"*/tests/*" \
"/Applications/*" \
"/Library/*" \
"/opt/homebrew/*" \
--gcov-tool "${GCOV}" \
--ignore-errors unused \
--output-file "${COVERAGE_FILTERED_INFO}"
echo "Generating HTML report"
genhtml "${COVERAGE_FILTERED_INFO}" \
--output-directory "${REPORT_DIR}"
REPORT_INDEX="${REPO_ROOT}/${REPORT_DIR}/index.html"
echo
echo "Done."
echo "CTest report: ${REPO_ROOT}/${CTEST_REPORT}"
echo "Coverage report: ${REPORT_INDEX}"
if [[ "${OPEN_REPORT}" -eq 1 ]]; then
open "${REPORT_INDEX}"
fi
它会自动完成:
- 选择 Homebrew 安装的最新 gcc/g++/gcov
- 配置 build-coverage
- 使用 --coverage -O0 -g 构建项目
- 运行全部 ctest
- 生成 CTest XML 报告
- 用 lcov + genhtml 生成 HTML 覆盖率报告
使用方法:
chmod +x ./scripts/run_coverage.sh
./scripts/run_coverage.sh
如果想清理后重新构建:
./scripts/run_coverage.sh --clean
如果想生成后自动打开 HTML 报告:
./scripts/run_coverage.sh --open
执行结果:
Using GCC 15
Configuring build-coverage
-- Configuring done (0.4s)
-- Generating done (0.4s)
-- Build files have been written to: /Users/xiye/CppProjects/googletest-example/build-coverage
Building
[ 2%] Building CXX object CMakeFiles/example_lib.dir/src/calculator.cpp.o
[ 4%] Building CXX object CMakeFiles/example_lib.dir/src/max.cpp.o
[ 7%] Linking CXX static library libexample_lib.a
[ 7%] Built target example_lib
[ 9%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[ 12%] Linking CXX executable main
[ 12%] Built target main
[ 14%] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest.dir/src/gtest-all.cc.o
[ 17%] Linking CXX static library ../../../lib/libgtest.a
[ 17%] Built target gtest
[ 19%] Building CXX object CMakeFiles/test_main.dir/tests/test_main.cpp.o
[ 21%] Linking CXX executable test_main
[ 21%] Built target test_main
[ 24%] Building CXX object _deps/googletest-build/googletest/CMakeFiles/gtest_main.dir/src/gtest_main.cc.o
[ 26%] Linking CXX static library ../../../lib/libgtest_main.a
[ 26%] Built target gtest_main
[ 29%] Building CXX object CMakeFiles/test_max.dir/tests/test_max.cpp.o
[ 31%] Linking CXX executable test_max
[ 31%] Built target test_max
[ 34%] Building CXX object CMakeFiles/test_min.dir/tests/test_min.cpp.o
[ 36%] Linking CXX executable test_min
[ 36%] Built target test_min
[ 39%] Building CXX object CMakeFiles/test_prime.dir/tests/test_prime.cpp.o
[ 41%] Linking CXX executable test_prime
[ 41%] Built target test_prime
[ 43%] Building CXX object CMakeFiles/test_suite.dir/tests/test_suite.cpp.o
[ 46%] Linking CXX executable test_suite
[ 46%] Built target test_suite
[ 48%] Building CXX object CMakeFiles/test_calculator.dir/tests/test_calculator.cpp.o
[ 51%] Linking CXX executable test_calculator
[ 51%] Built target test_calculator
[ 53%] Building CXX object CMakeFiles/test_container.dir/tests/test_container.cpp.o
[ 56%] Linking CXX executable test_container
[ 56%] Built target test_container
[ 58%] Building CXX object CMakeFiles/test_numeric.dir/tests/test_numeric.cpp.o
[ 60%] Linking CXX executable test_numeric
[ 60%] Built target test_numeric
[ 63%] Building CXX object CMakeFiles/test_queue.dir/tests/test_queue.cpp.o
[ 65%] Linking CXX executable test_queue
[ 65%] Built target test_queue
[ 68%] Building CXX object CMakeFiles/test_bank_vault.dir/tests/test_bank_vault.cpp.o
[ 70%] Linking CXX executable test_bank_vault
[ 70%] Built target test_bank_vault
[ 73%] Building CXX object CMakeFiles/test_utility.dir/tests/test_utility.cpp.o
[ 75%] Linking CXX executable test_utility
[ 75%] Built target test_utility
[ 78%] Building CXX object CMakeFiles/test_database.dir/tests/test_database.cpp.o
[ 80%] Linking CXX executable test_database
[ 80%] Built target test_database
[ 82%] Building CXX object CMakeFiles/test_add_param.dir/tests/test_add_param.cpp.o
[ 85%] Linking CXX executable test_add_param
[ 85%] Built target test_add_param
[ 87%] Building CXX object _deps/googletest-build/googlemock/CMakeFiles/gmock.dir/src/gmock-all.cc.o
[ 90%] Linking CXX static library ../../../lib/libgmock.a
[ 90%] Built target gmock
[ 92%] Building CXX object _deps/googletest-build/googlemock/CMakeFiles/gmock_main.dir/src/gmock_main.cc.o
[ 95%] Linking CXX static library ../../../lib/libgmock_main.a
[ 95%] Built target gmock_main
[ 97%] Building CXX object CMakeFiles/test_network_client.dir/tests/test_network_client.cpp.o
[100%] Linking CXX executable test_network_client
[100%] Built target test_network_client
Resetting coverage counters
Running tests
Test project /Users/xiye/CppProjects/googletest-example/build-coverage
Start 1: test_main
1/14 Test #1: test_main ........................ Passed 0.51 sec
Start 2: test_max
2/14 Test #2: test_max ......................... Passed 0.35 sec
Start 3: test_min
3/14 Test #3: test_min ......................... Passed 0.35 sec
Start 4: test_prime
4/14 Test #4: test_prime ....................... Passed 0.35 sec
Start 5: test_suite
5/14 Test #5: test_suite ....................... Passed 0.35 sec
Start 6: test_calculator
6/14 Test #6: test_calculator .................. Passed 0.36 sec
Start 7: test_container
7/14 Test #7: test_container ................... Passed 0.35 sec
Start 8: test_numeric
8/14 Test #8: test_numeric ..................... Passed 0.51 sec
Start 9: test_queue
9/14 Test #9: test_queue ....................... Passed 0.37 sec
Start 10: test_bank_vault
10/14 Test #10: test_bank_vault .................. Passed 0.35 sec
Start 11: test_utility
11/14 Test #11: test_utility ..................... Passed 0.36 sec
Start 12: test_database
12/14 Test #12: test_database .................... Passed 0.36 sec
Start 13: test_add_param
13/14 Test #13: test_add_param ................... Passed 0.36 sec
Start 14: test_network_client
14/14 Test #14: test_network_client .............. Passed 0.37 sec
100% tests passed, 0 tests failed out of 14
Total Test time (real) = 5.33 sec
Capturing coverage
Capturing coverage data from build-coverage
geninfo cmd: '/opt/homebrew/Cellar/lcov/2.4_1/bin/geninfo build-coverage --toolname lcov --output-filename build-coverage/coverage.info --ignore-errors inconsistent --gcov-tool /opt/homebrew/bin/gcov-15'
Found gcov version: 15.2.0
Using intermediate gcov format
Recording 'internal' directories:
/Users/xiye/CppProjects/googletest-example/build-coverage
build-coverage
Writing temporary data to /tmp/geninfo_dat4UYm
Scanning build-coverage for .gcda files ...
Found 20 data files in build-coverage
using: chunkSize: 1, nchunks:20, intervalLength:1
lcov: WARNING: (inconsistent) mismatched end line for _ZN39BankVaultTest_AccessPrivateMembers_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_bank_vault.cpp:4: 4 -> 9 while capturing from build-coverage/CMakeFiles/test_bank_vault.dir/tests/test_bank_vault.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN41BankVaultTest_TestPasswordValidation_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_bank_vault.cpp:11: 11 -> 16 while capturing from build-coverage/CMakeFiles/test_bank_vault.dir/tests/test_bank_vault.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN40BankVaultTest_DISABLED_TestWithdraw_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_bank_vault.cpp:18: 18 -> 24 while capturing from build-coverage/CMakeFiles/test_bank_vault.dir/tests/test_bank_vault.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN35AddTest_HandlesDifferentInputs_Test13AddToRegistryEv at /Users/xiye/CppProjects/googletest-example/tests/test_add_param.cpp:16: 20 -> 16 while capturing from build-coverage/CMakeFiles/test_add_param.dir/tests/test_add_param.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN35AddTest_HandlesDifferentInputs_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_add_param.cpp:16: 20 -> 16 while capturing from build-coverage/CMakeFiles/test_add_param.dir/tests/test_add_param.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN36UtilityTest_PrivateStaticMethod_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_utility.cpp:4: 4 -> 8 while capturing from build-coverage/CMakeFiles/test_utility.dir/tests/test_utility.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN18FooTest_Test1_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_suite.cpp:25: 25 -> 28 while capturing from build-coverage/CMakeFiles/test_suite.dir/tests/test_suite.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN18FooTest_Test2_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_suite.cpp:30: 30 -> 33 while capturing from build-coverage/CMakeFiles/test_suite.dir/tests/test_suite.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MaxTest_NegativeNumbers_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:5: 5 -> 8 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MaxTest_NegativeNumbers_TestD0Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:5: 8 -> 5 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MaxTest_NegativeNumbers_TestD1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:5: 8 -> 5 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN25MaxTest_MixedNumbers_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:10: 10 -> 13 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN25MaxTest_MixedNumbers_TestD1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:10: 13 -> 10 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN25MaxTest_MixedNumbers_TestD0Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:10: 13 -> 10 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MinTest_PositiveNumbers_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:16: 16 -> 19 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MinTest_PositiveNumbers_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:16: 19 -> 16 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MinTest_NegativeNumbers_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:21: 21 -> 24 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN28MinTest_NegativeNumbers_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp:21: 24 -> 21 while capturing from build-coverage/CMakeFiles/test_main.dir/tests/test_main.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN19Max_MaxOf3And5_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_max.cpp:4: 4 -> 7 while capturing from build-coverage/CMakeFiles/test_max.dir/tests/test_max.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN20Max_MaxOf10And5_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_max.cpp:9: 9 -> 12 while capturing from build-coverage/CMakeFiles/test_max.dir/tests/test_max.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN38NetworkClientTest_SendDataSuccess_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_network_client.cpp:8: 8 -> 16 while capturing from build-coverage/CMakeFiles/test_network_client.dir/tests/test_network_client.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN38NetworkClientTest_SendDataFailure_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_network_client.cpp:18: 18 -> 26 while capturing from build-coverage/CMakeFiles/test_network_client.dir/tests/test_network_client.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN30DatabaseTest_InsertRecord_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_database.cpp:59: 59 -> 62 while capturing from build-coverage/CMakeFiles/test_database.dir/tests/test_database.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN30DatabaseTest_DeleteRecord_Test8TestBodyEv at /Users/xiye/CppProjects/googletest-example/tests/test_database.cpp:64: 64 -> 66 while capturing from build-coverage/CMakeFiles/test_database.dir/tests/test_database.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN30CalculatorTest_Operations_Test13AddToRegistryEv at /Users/xiye/CppProjects/googletest-example/tests/test_calculator.cpp:8: 25 -> 8 while capturing from build-coverage/CMakeFiles/test_calculator.dir/tests/test_calculator.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN30CalculatorTest_Operations_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_calculator.cpp:8: 25 -> 8 while capturing from build-coverage/CMakeFiles/test_calculator.dir/tests/test_calculator.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN38CalculatorCombinedTest_Operations_Test13AddToRegistryEv at /Users/xiye/CppProjects/googletest-example/tests/test_calculator.cpp:42: 63 -> 42 while capturing from build-coverage/CMakeFiles/test_calculator.dir/tests/test_calculator.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN38CalculatorCombinedTest_Operations_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_calculator.cpp:42: 63 -> 42 while capturing from build-coverage/CMakeFiles/test_calculator.dir/tests/test_calculator.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN29PrimeTest_CheckPrimality_Test13AddToRegistryEv at /Users/xiye/CppProjects/googletest-example/tests/test_prime.cpp:16: 23 -> 16 while capturing from build-coverage/CMakeFiles/test_prime.dir/tests/test_prime.cpp.gcda
lcov: WARNING: (inconsistent) mismatched end line for _ZN29PrimeTest_CheckPrimality_TestC1Ev at /Users/xiye/CppProjects/googletest-example/tests/test_prime.cpp:16: 23 -> 16 while capturing from build-coverage/CMakeFiles/test_prime.dir/tests/test_prime.cpp.gcda
Finished processing 20 GCDA files
Apply filtering..
Finished filter file processing
Finished .info-file creation
Summary coverage rate:
source files: 122
lines.......: 47.4% (4716 of 9945 lines)
functions...: 61.6% (4014 of 6512 functions)
Message summary:
30 warning messages:
inconsistent: 30
1 ignore message:
inconsistent: 1
Filtering coverage
Excluding /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/usr/include/___wctype.h
Excluding /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/usr/include/_ctype.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/include/gmock/gmock-actions.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/include/gmock/gmock-cardinalities.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/include/gmock/gmock-matchers.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/include/gmock/gmock-spec-builders.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/include/gmock/internal/gmock-internal-utils.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock-cardinalities.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock-internal-utils.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock-matchers.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock-spec-builders.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googlemock/src/gmock_main.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-assertion-result.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-matchers.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-message.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-param-test.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-printers.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest-test-part.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/gtest.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-death-test-internal.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-filepath.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-internal.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-param-util.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-port.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/include/gtest/internal/gtest-type-util.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-assertion-result.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-death-test.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-filepath.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-internal-inl.h
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-matchers.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-port.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-printers.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-test-part.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest-typed-test.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest.cc
Excluding /Users/xiye/CppProjects/googletest-example/build-coverage/_deps/googletest-src/googletest/src/gtest_main.cc
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_add_param.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_bank_vault.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_calculator.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_container.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_database.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_main.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_max.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_min.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_network_client.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_numeric.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_prime.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_queue.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_suite.cpp
Excluding /Users/xiye/CppProjects/googletest-example/tests/test_utility.cpp
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/aarch64-apple-darwin24/bits/c++config.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/aarch64-apple-darwin24/bits/gthr-default.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/array
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/atomic
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/alloc_traits.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/allocated_ptr.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/allocator.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/atomic_base.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/basic_string.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/basic_string.tcc
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/char_traits.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/charconv.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/chrono.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/cpp_type_traits.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/deque.tcc
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/functional_hash.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/hashtable.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/hashtable_policy.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/invoke.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ios_base.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/list.tcc
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/move.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/new_allocator.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/predefined_ops.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/ptr_traits.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/shared_ptr.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/shared_ptr_base.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/std_function.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algo.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_construct.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_deque.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_function.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_iterator.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_iterator_base_funcs.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_iterator_base_types.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_list.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_map.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_pair.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_queue.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_set.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_tempbuf.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_tree.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_uninitialized.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_vector.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/string_view.tcc
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/unique_ptr.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/unordered_map.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/unordered_set.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/vector.tcc
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/cmath
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/ext/aligned_buffer.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/ext/alloc_traits.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/ext/atomicity.h
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/initializer_list
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/iomanip
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/iostream
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/limits
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/new
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/string_view
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/tuple
Excluding /opt/homebrew/Cellar/gcc/15.2.0_1/include/c++/15/typeinfo
Removed 113 files
Writing data to build-coverage/coverage.filtered.info
Summary coverage rate:
source files: 9
lines.......: 74.4% (32 of 43 lines)
functions...: 90.5% (19 of 21 functions)
lcov: WARNING: (unused) 'exclude' pattern '/Applications/*' is unused.
(use "lcov --ignore-errors unused,unused ..." to suppress this warning)
Message summary:
1 warning message:
unused: 1
Generating HTML report
Reading tracefile build-coverage/coverage.filtered.info.
Found 9 entries.
Found common filename prefix "/Users/xiye/CppProjects/googletest-example"
Generating output.
Processing file include/environment.h
lines=6 hit=6 functions=2 hit=2
Processing file include/bank_vault.h
lines=8 hit=3 functions=3 hit=2
Processing file include/mock_network_interface.h
lines=1 hit=1 functions=2 hit=2
Processing file include/min.h
lines=2 hit=2 functions=3 hit=3
Processing file include/network_interface.h
lines=1 hit=1 functions=1 hit=1
Processing file include/network_client.h
lines=5 hit=4 functions=2 hit=2
Processing file src/calculator.cpp
lines=14 hit=9 functions=5 hit=4
Processing file include/utility.h
lines=4 hit=4 functions=2 hit=2
Processing file src/max.cpp
lines=2 hit=2 functions=1 hit=1
Overall coverage rate:
source files: 9
lines.......: 74.4% (32 of 43 lines)
functions...: 90.5% (19 of 21 functions)
Message summary:
no messages were reported
Done.
CTest report: /Users/xiye/CppProjects/googletest-example/build-coverage/ctest-report.xml
Coverage report: /Users/xiye/CppProjects/googletest-example/build-coverage/coverage-report/index.html
生成文件位置:
- build-coverage/ctest-report.xml
- build-coverage/coverage-report/index.html
使用持续集成(CI)工具
将测试流程集成到持续集成系统中,实现每次代码提交后自动运行测试。常用的 CI 工具包括 GitHub Actions、Jenkins、Travis CI 等。
示例:使用 GitHub Actions
创建 .github/workflows/ci.yml 文件:
name: CI
on:
push:
pull_request:
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake
- name: Configure
run: cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug
- name: Build
run: cmake --build build --parallel
- name: Run tests
run: ctest --test-dir build --output-on-failure
触发条件:每次代码推送或拉取请求时触发
- push
- pull_request
CI 步骤:
- 检出代码
- 安装 build-essential 和 cmake
- 配置 CMake
- 构建项目
- 运行 ctest
代码推送后,可以看到测试自动化结果:

定时自动化测试
设定定时任务(如 Cron 作业),定期运行测试,确保项目在长时间运行中的稳定性。
示例:Cron 作业
编辑 Crontab 文件:
crontab -e
添加以下行,每天凌晨 2 点运行测试脚本:
0 2 * * * /scripts/run_coverage.sh >> /scripts/run_coverage.sh 2>&1
3092

被折叠的 条评论
为什么被折叠?



