关于单元测试(googletest)和项目/文件夹/文件的困惑 [英] Confusion about unit tests (googletest) and projects/folder/files

查看:837
本文介绍了关于单元测试(googletest)和项目/文件夹/文件的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在我的c ++项目中使用单元测试。因此,我有许多现有的类,我将编写测试(至少对于其中一些)。此外,应用程序当然有一个 main()函数。

It's the first time I want to use unit tests in my c++ project. Hence I've many existing classes for which I will write tests (at least for some of them). Further, the application has of course a main() function.

我正在使用qt创建者和qmake但是也可以切换到cmake。该项目,qt creator和qmake运行良好。

I'm using qt creator with qmake but could also switch to cmake. The project, qt creator and qmake are working nicely.

我现在的困惑是如何添加单元测试?我打算使用googletest。我已经在一个新项目中运行了一个测试,测试了一些虚拟的 add(int,int)函数,其中包含一个文件(函数,测试和main)。如何使用现有项目(它有自己的 main())。我是否需要设置第二个项目并在测试文件中包含标题?什么是良好的文件夹结构?

My confusion is now how do I add unit test? I intent to use googletest. I've already run a test in a new project, testing some dummy add(int, int) function, with everything in one file (function, tests and main). How does that work with an existing project (which has it's own main()). Do I need to setup a second project and include the headers in the test files? What is a good folder structure for that?

推荐答案

在我看来,最好的方法是创建一个模型库(包含所有生产代码),程序可执行文件测试可执行文件。然后,您可以将所有生产代码链接到程序和测试可执行文件。测试文件也存储在测试可执行文件中。我的所有项目都有这样的结构:

In my opinion the best approach is to create a model library (with all the production code), a program executable and a test executable. Then you can link all your production code against the program and test executable. The test-files are also stored in the test executable. All my projects have this structure:

model.lib (link against both exe)
program.exe
modelTest.exe

在文件系统的具体文件夹中可以存储测试和生产 - 文件。构建工具(如cmake)应该将文件分开,并将测试文件放入测试可执行文件,将生产文件放入模型库中。

In the concrete folder on your file system can be stored the test- and production-files. A build tool (like cmake) should separate the files and put the test files into the test executable and the production files into the model-library.

请考虑以下示例:
我有一个包含以下文件的文件夹:

Consider the following example: I have a folder with the following files:

src (folder)
 - main.cpp
 - model.h
 - model.cpp
 - modelTest.cpp

cmake文件可能如下所示:

A cmake file could look like this:

cmake_minimum_required(VERSION 2.8)
project(TheUltimateProject)
ADD_EXECUTABLE(program main.cpp)
ADD_library(model shared model.cpp model.h)
ADD_EXECUTABLE(modelTest modelTest.cpp)

target_link_libraries(program model)
target_link_libraries(modelTest model)

如果您使用像google test这样的测试框架,您还需要将modelTest可执行文件链接到gmock并且不要忘记添加include文件夹:

If you use a testing-framework like google test, you also have to link the modelTest executable against gmock and don't forget to add the include folder:

例如

link_directories($ENV{GMOCK_HOME}/Debug)
include_directories($ENV{GMOCK_HOME}/googlemock/include) 
include_directories($ENV{GMOCK_HOME}/googletest/include)
target_link_libraries(modelTest gmock_main gmock)

这篇关于关于单元测试(googletest)和项目/文件夹/文件的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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