#mac下 CLion gtest 单元测试环境搭建
1,下载gtest源码,将下载解压后的文件放到CLion工程目录中,如下所示:
2,修改CMakeList.txt文件,内容如下:
cmake_minimum_required(VERSION 3.9)
project(unit_test)
set(CMAKE_CXX_STANDARD 14)
include_directories(${PROJECT_SOURCE_DIR}/gtest-1.7.0/include/)
add_subdirectory(./gtest-1.7.0)
set(3RD_LIBS gtest pthread)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${3RD_LIBS})
3,添加测试用例,如下:
#include <iostream>
#include <gtest/gtest.h>
int Add(int a, int b) {
return a+b;
}
TEST(testCase, test0){
EXPECT_EQ(14, Add(4, 10));
}
TEST(testCase, test1){
ASSERT_EQ(6, Add(5, 7));
}
TEST(testCase, test2){
EXPECT_EQ(28, Add(8, 10)) << "bugs";
}
int main(int argc, char ** argv) {
std::cout << "Hello, World!" << std::endl;
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS(); // 执行所有的 test case
}
4,运行测试用例,如下图:
5,控制台会输出测试用例的执行结果。
gtest的详细用法请看:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html