git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cd build
cmake ..
make -j4
sudo make install
project(test)
set(CMAKE_CXX_STANDARD 11)
#cmake_minimum_required(VERSION 3.5)
aux_source_directory(./ SRC)
message("project for test is ${PROJECT_NAME}")
add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} gtest pthread)
#target_include_directories(${PROJECT_NAME}
#)
├── main.cpp
├── test
│ ├── CMakeLists.txt
│ └── test.cpp
└── utils
├── CMakeLists.txt
├── common.cpp
└── common.h
#include <iostream>
#include <gtest/gtest.h>
TEST(HelloTest, BasicAssertions) {
EXPECT_STRNE("hello", "world");
EXPECT_EQ(6 * 7, 42);
}
//xxxxxx自己的test程序
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
打开–g3 选项,去掉-O2以上级别的代码优化选项;否则编译器会对代码做一些优化,例如行合并,从而影响行覆盖率结果;这里我比较懒就没有加
加到test目录下的CMakeLists.txt即可 , 其中代码编译完之后会在test/CMakeFiles/test.dir/ 生成test.cpp.gcno文件, 在运行完test程序之后会在目录下生成test.cpp.gcda文件
# coverage option
OPTION (ENABLE_COVERAGE "Use gcov" ON)
MESSAGE(STATUS ENABLE_COVERAGE=${ENABLE_COVERAGE})
IF(ENABLE_COVERAGE)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
ENDIF()
代码中的/home/liar/work2/ 为代码根目录, 请根据自己的目录进行替换
cd build/test #进入编译后的test目录
lcov -d /home/liar/work2/ -t test -o test.info -b . -c --no-external #
genhtml -o report test.info #
**生成test.info文件, **
test.info 生成report文件夹, 在report文件夹下的index.html 就是覆盖率报告