https://github.com/google/googletest
下载gtest源码编译安装
mkdir build && cd build && cmake .. && make -j4 && make install
直接make install到系统指定的用户头文件和库所在路径,方便后面直接用
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Aut)
add_executable(Aut
Aut.cpp
../src/A.cpp)
target_include_directories(Aut PUBLIC
"../include"
)
target_link_libraries(Aut PUBLIC gtest_main gtest pthread)
include/A.h
class A
{
public:
int test(void);
};
src/A.cpp
#include "A.h"
#include <cstdio>
int A::test(void)
{
printf("normal test");
return 0;
}
main.cpp
#include <cstdio>
#include "A.h"
int main()
{
A a;
int ret = a.test();
printf("ret = %d
");
return 0;
}
ut/Aut.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "Aut.h"
TEST(footest, test)
{
A a;
EXPECT_EQ(a.test(), 0);
}
ut/Aut.h
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "A.h"
class footest : public testing::Test
{
};
ut/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Aut)
add_executable(Aut
Aut.cpp
../src/A.cpp)
target_include_directories(Aut PUBLIC
"../include"
)
target_link_libraries(Aut PUBLIC gtest_main gtest pthread)
main可以直接在外面mkdir build && cd build && cmake .. && make -j4
ut可以直接进入到ut目录下mkdir build && cd build && cmake .. && make -j4
上面就是简单的使用gtest做单元测试demo。