我试图弄清楚如何使用 CMake 针对我的 C++ 项目运行 Google Test。到目前为止,我已经创建了一个名为 Simple 的项目和一个名为 SimpleTest 的 Google 测试项目。
对于简单的项目
这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.8.4)
project(Simple)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
main.cpp
NewCppClass.cpp
NewCppClass.h)
add_executable(Simple ${SOURCE_FILES})
这是我的 main.cpp 文件:
#include <iostream>
#include "NewCppClass.h"
using namespace std;
int main() {
NewCppClass newCppClass;
int i = newCppClass.getNumberToTest();
cout << "i = " << i;
return 0;
}
这是我的类标题:
#ifndef SIMPLE_NEWCPPCLASS_H
#define SIMPLE_NEWCPPCLASS_H
class NewCppClass {
public:
int getNumberToTest();
};
#endif //SIMPLE_NEWCPPCLASS_H
这是我的 .cpp 文件:
#include "NewCppClass.h"
int NewCppClass::getNumberToTest() {
return 5;
}
对于 SimpleTest 项目
这是我的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 2.8.4)
project(SimpleTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES
Main_TestAll.cpp
MyFirstTest.cpp)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(SimpleTest ${SOURCE_FILES})
target_link_libraries(SimpleTest ${GTEST_BOTH_LIBRARIES})
这是我的 Main_TestAll.cpp 文件:
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
这是 MyFirstTest.cpp 文件:
当然,当我弄清楚如何指向时,这个包含必须改变
我的简单项目。
#include "this/package/NewCppClass.h"
#include "gtest/gtest.h"
namespace {
// The fixture for testing class NewCppClass.
class NewCppClassTest : public ::testing::Test {
protected:
// You can remove any or all of the following functions if its body
// is empty.
NewCppClassTest() {
// You can do set-up work for each test here.
}
virtual ~NewCppClassTest() {
// You can do clean-up work that doesn't throw exceptions here.
}
// If the constructor and destructor are not enough for setting up
// and cleaning up each test, you can define the following methods:
virtual void SetUp() {
// Code here will be called immediately after the constructor (right
// before each test).
}
virtual void TearDown() {
// Code here will be called immediately after each test (right
// before the destructor).
}
// Objects declared here can be used by all tests in the test case for Foo.
};
// Tests that NewCppClass::getNumberToTest() is not equal to this fixed mumber.
TEST_F(NewCppClassTest, ThisTestShallFail) {
NewCppClass newCppClass;
int i = newCppClass.getNumberToTest();
EXPECT_EQ(i, 2);
}
} // namespace
UPDATE:
πάντα-ῥεῖ 写了这个question:
我建议将所有测试用例类(作为普通的 .cpp
源)到一个单独的项目中,并与被测试的类链接
来自一个单独的图书馆项目。将 gtest_all.cc 包含在 main() 中
函数,或与测试项目链接到 gtest 库。
要运行测试用例,请添加运行 UnitTester 工件构建
该项目作为额外的构建步骤。
我认为这是正确的方向,所以我将其添加到问题中以提醒自己,并且可能对其他人有帮助。
同样在下面,由 πάντα-ῥεῖ 撰写:
...被测试的类应该捆绑到一个单独的库中
工件,并链接到测试运行程序应用程序。
当我尝试在脑海中整理需要做什么时,我将所有这些信息都包含在这里。
如果我了解需要正确完成哪些操作,那么我需要(在我的 C++ 项目中)添加到 CMakeLists.txt 文件以将 GTest 添加为外部项目,并在 add_executable 中添加测试。像这样的事情:
################################
# GTest
################################
include(ExternalProject)
enable_testing()
find_package(GTest REQUIRED)
################################
# Unit Tests
################################
# Add test cpp file
# Link test executable against gtest & gtest_main
add_executable(SimpleTest
Main_TestAll.cpp
MyFirstTest.cpp)
target_link_libraries(Test GTest)
add_test( runUnitTests runUnitTests )