Google Test单独项目 - 如何获得针对C ++项目运行的测试 [英] Google Test separate project - How to get tests running against the C++ project

查看:612
本文介绍了Google Test单独项目 - 如何获得针对C ++项目运行的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何使用CMake对我的C ++项目运行Google Test。到目前为止,我创建了一个名为Simple的项目,名为SimpleTest的Google测试项目。

I am trying to figure out how to run Google Test against my C++ project using CMake. So far I have created a project called Simple and a Google Test project called SimpleTest.

对于简单项目

这是我的CMakeLists.txt文件:

Here's my CMakeLists.txt file:

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文件:

Here's my main.cpp file:

#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文件: / p>

Here is my .cpp file:

#include "NewCppClass.h"

int NewCppClass::getNumberToTest() {
    return 5;
}

对于SimpleTest项目

这是我的CMakeLists.txt文件:

Here's my CMakeLists.txt file:

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文件:

Here's my Main_TestAll.cpp file:

#include "gtest/gtest.h"

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

这里是MyFirstTest.cpp文件:

Here is MyFirstTest.cpp file:


当然,当我想出如何指向
my Simple项目时,这个include必须改变。

Of course, this include must change when I figure out how to point to my Simple project.



#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

πάντα-ῥεῖ写了此问题

πάντα-ῥεῖ wrote this question:


我建议把所有的测试用例类b $ b sources)到一个单独的项目中,并从一个单独的库项目链接到测试
下的类。包含带有main()
函数的gtest_all.cc,或者与测试项目的gtest库链接。

I would recommend to put all the test case classes (as plain .cpp sources) into a separate project, and link with the classes under test from a separate library project. Include gtest_all.cc with the main() function, or link against the gtest library, with the test project.

要运行测试用例,从
生成UnitTester工件作为附加构建步骤。

To run the test cases add running the UnitTester artifact build from that project as an additional build step.

我认为这是正确的方向,

I think this is the correct direction, so I'm adding it to the question as a reminder to myself and it may be helpful to others.

在下面,由πάντα-ῥ,,写:

Also in below, written by πάντα-ῥεῖ:


...测试中的类应该捆绑到一个单独的库
artifact中,并链接到测试运行器应用程序。

...the classes under test should be bundled into a separate library artifact, and linked to the test runner application.

我在这里包含所有这些信息,因为我试图在我的脑海中编译需要做的事。

I'm including all of this information here as I try to compile in my mind what needs to be done.

如果我明白需要做什么正确,那么我需要(在我的C ++项目中)添加到CMakeLists.txt文件以添加GTest作为ExternalProject在add_executable中添加测试。像这样:

If I understand what needs to be done correctly, then I need to (in my C++ project) add to the CMakeLists.txt file to add GTest as an ExternalProject and add the tests in add_executable. Something like this:

################################
# 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 )


推荐答案

问题似乎是你的代码模块的组织。假设你有一个c ++目标项目,它提供一个可执行程序作为它的最终输出:

The problem seems to be the organization of your code modules. Supposed you have a c++ target project, that provides an executable program as it's final output:


  • 我想你想创建两个可执行文件

    • 您的最终应用

    • 运行您指定的所有测试用例的测试运行器应用


    • 在库和链接中提供独立的核心类它到你的最终应用程序和测试运行器。应用程序应该从 main()入口点提供一个thin包装。

    • 添加指向测试类的源的链接,并在测试运行程序环境中完全编译

    • provide the core classes separate in a library and link it to your final application and the test runner. The application should provide a thin wrapper from the main() entry point.
    • add links to the sources for the classes under test, and completely compile them in the test runner environment

    这篇关于Google Test单独项目 - 如何获得针对C ++项目运行的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆