Pat*_*cia 3 c++ cmake googletest clion
我想弄清楚如何使用 CMake 对我的 C++ 项目运行 Google 测试。到目前为止,我已经创建了一个名为 Simple 的项目和一个名为 SimpleTest 的 Google Test 项目。
对于简单项目
这是我的 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})
Run Code Online (Sandbox Code Playgroud)
这是我的 main.cpp 文件:
#include <iostream>
#include "NewCppClass.h"
using namespace std;
int main() {
NewCppClass newCppClass;
int i = newCppClass.getNumberToTest();
cout << "i = " << i;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我的类标题:
#ifndef SIMPLE_NEWCPPCLASS_H
#define SIMPLE_NEWCPPCLASS_H
class NewCppClass {
public:
int getNumberToTest();
};
#endif //SIMPLE_NEWCPPCLASS_H
Run Code Online (Sandbox Code Playgroud)
这是我的 .cpp 文件:
#include "NewCppClass.h"
int NewCppClass::getNumberToTest() {
return 5;
}
Run Code Online (Sandbox Code Playgroud)
对于 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})
Run Code Online (Sandbox Code Playgroud)
这是我的 Main_TestAll.cpp 文件:
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Run Code Online (Sandbox Code Playgroud)
这是 MyFirstTest.cpp 文件:
当然,当我弄清楚如何指向我的 Simple 项目时,这包括必须改变。
#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
Run Code Online (Sandbox Code Playgroud)
更新:
??????-??? 写了这个问题:
我建议将所有测试用例类(作为纯 .cpp 源)放入一个单独的项目中,并与来自单独库项目的测试类链接。在 main() 函数中包含 gtest_all.cc,或通过测试项目链接 gtest 库。
要运行测试用例,请添加从该项目运行 UnitTester 工件构建作为附加构建步骤。
我认为这是正确的方向,所以我将它添加到问题中作为对自己的提醒,它可能对其他人有所帮助。
同样在下面,由 ?????-??? 编写:
...测试中的类应该捆绑到一个单独的库工件中,并链接到测试运行器应用程序。
我将所有这些信息都包括在这里,因为我试图在脑海中整理需要做的事情。
如果我了解需要正确完成哪些操作,那么我需要(在我的 C++ 项目中)添加到 CMakeLists.txt 文件以将 GTest 添加为 ExternalProject 并在 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 )
Run Code Online (Sandbox Code Playgroud)
问题似乎出在代码模块的组织上。假设您有一个 C++ 目标项目,它提供了一个可执行程序作为最终输出:
main()入口点提供一个瘦包装器。| 归档时间: |
|
| 查看次数: |
3297 次 |
| 最近记录: |