我决定快速地将一些通用的东西放在一起,以演示与以前发布的答案不同的方式,希望它能对某人有所帮助。以下在我的Mac上为我工作。首先,我为gtests运行了设置命令。我只是使用发现的脚本来设置所有内容。
#!/usr/bin/env bash
# install gtests script on mac
# https://gist.github.com/butuzov/e7df782c31171f9563057871d0ae444a
#usage
# chmod +x ./gtest_installer.sh
# sudo ./gtest_installer.sh
# Current directory
__THIS_DIR=$(pwd)
# Downloads the 1.8.0 to disc
function dl {
printf "\n Downloading Google Test Archive\n\n"
curl -LO https://github.com/google/googletest/archive/release-1.8.0.tar.gz
tar xf release-1.8.0.tar.gz
}
# Unpack and Build
function build {
printf "\n Building GTest and Gmock\n\n"
cd googletest-release-1.8.0
mkdir build
cd $_
cmake -Dgtest_build_samples=OFF -Dgtest_build_tests=OFF ../
make
}
# Install header files and library
function install {
printf "\n Installing GTest and Gmock\n\n"
USR_LOCAL_INC="/usr/local/include"
GTEST_DIR="/usr/local/Cellar/gtest/"
GMOCK_DIR="/usr/local/Cellar/gmock/"
mkdir $GTEST_DIR
cp googlemock/gtest/*.a $GTEST_DIR
cp -r ../googletest/include/gtest/ $GTEST_DIR
ln -snf $GTEST_DIR $USR_LOCAL_INC/gtest
ln -snf $USR_LOCAL_INC/gtest/libgtest.a /usr/local/lib/libgtest.a
ln -snf $USR_LOCAL_INC/gtest/libgtest_main.a /usr/local/lib/libgtest_main.a
mkdir $GMOCK_DIR
cp googlemock/*.a $GMOCK_DIR
cp -r ../googlemock/include/gmock/ $GMOCK_DIR
ln -snf $GMOCK_DIR $USR_LOCAL_INC/gmock
ln -snf $USR_LOCAL_INC/gmock/libgmock.a /usr/local/lib/libgmock.a
ln -snf $USR_LOCAL_INC/gmock/libgmock_main.a /usr/local/lib/libgmock_main.a
}
# Final Clean up.
function cleanup {
printf "\n Running Cleanup\n\n"
cd $__THIS_DIR
rm -rf $(pwd)/googletest-release-1.8.0
unlink $(pwd)/release-1.8.0.tar.gz
}
dl && build && install && cleanup
接下来,我制作了一个简单的文件夹结构,并编写了一些快速的类
utils/
cStringUtils.cpp
cStringUtils.h
CMakeLists.txt
utils/tests/
gtestsMain.cpp
cStringUtilsTest.cpp
CMakeLists.txt
我为utils文件夹做了一个顶层CMakeLists.txt,为tests文件夹做了一个CMakeLists.txt。
cmake_minimum_required(VERSION 2.6)
project(${GTEST_PROJECT} C CXX)
set(CMAKE_C_STANDARD 98)
set(CMAKE_CXX_STANDARD 98)
#include .h and .cpp files in util folder
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
##########
# GTests
#########
add_subdirectory(tests)
这是测试文件夹中的CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
set(GTEST_PROJECT gtestProject)
enable_testing()
message("Gtest Cmake")
find_package(GTest REQUIRED)
# The utils, test, and gtests directories
include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
include_directories("/usr/local/Cellar/gtest/include")
include_directories("/usr/local/Cellar/gtest/lib")
set(SOURCES
gtestsMain.cpp
../cStringUtils.cpp
cStringUtilsTest.cpp
)
set(HEADERS
../cStringUtils.h
)
add_executable(${GTEST_PROJECT} ${SOURCES})
target_link_libraries(${GTEST_PROJECT} PUBLIC
gtest
gtest_main
)
add_test(${GTEST_PROJECT} ${GTEST_PROJECT})
然后剩下的就是编写示例gtest和gtest main
样本gtest
#include "gtest/gtest.h"
#include "cStringUtils.h"
namespace utils
{
class cStringUtilsTest : public ::testing::Test {
public:
cStringUtilsTest() : m_function_param(10) {}
~cStringUtilsTest(){}
protected:
virtual void SetUp()
{
// declare pointer
pFooObject = new StringUtilsC();
}
virtual void TearDown()
{
// Code here will be called immediately after each test
// (right before the destructor).
if (pFooObject != NULL)
{
delete pFooObject;
pFooObject = NULL;
}
}
StringUtilsC fooObject; // declare object
StringUtilsC *pFooObject;
int m_function_param; // this value is used to test constructor
};
TEST_F(cStringUtilsTest, testConstructors){
EXPECT_TRUE(1);
StringUtilsC fooObject2 = fooObject; // use copy constructor
fooObject.fooFunction(m_function_param);
pFooObject->fooFunction(m_function_param);
fooObject2.fooFunction(m_function_param);
}
} // utils end
gtest主样本
#include "gtest/gtest.h"
#include "cStringUtils.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
然后,我可以使用utils文件夹中的以下命令编译并运行gtests
cmake .
make
./tests/gtestProject
ExternalProject_Add
而不是避免自己包含GTest源代码add_subdirectory
。有关详细信息,请参见此答案。