如何在通过 QTestLib 对 Qt 应用程序进行单元测试时构建项目 [英] How to structure project while unit-testing Qt app by QTestLib
问题描述
我有我的 Qt 项目并且我正在使用 Qt Creator.我想对我所有的代码进行单元测试.
然而,我对 QTestLib 框架很陌生,但每个人都推荐它来测试基于 Qt 的源代码.现在我有点困惑如何使用应用项目构建测试项目.
I got my Qt project and I'm using Qt Creator. I want to unit-test all my code.
However I'm quite new at QTestLib framework but everyone recommended it for testing Qt-based source. Now I'm a little confused how to structure test project with app project.
- 我可以将所有源代码和测试代码放在同一个项目中吗?如果是这样,我该如何管理它们?我没有找到让我在一个项目中启动应用程序或开始测试的任何选项.
- 如果我把应用源代码和测试代码放在不同的项目中,测试项目会引用应用项目,这不太方便.
- 对于需要测试的类的批次,我如何管理测试代码?
在这种情况下,你们如何管理测试代码?谢谢.
How do you guys manage testing code in such a situation? Thanks.
推荐答案
第一个结构源如下:
MyApp
MyAppUnitTest
在MyApp
项目下,使用MyAppSrc.pri
来定位源文件:
Under MyApp
project, use a MyAppSrc.pri
to locate source files:
SOURCES += \
../../../framework/src/myapp.cpp \
../../../framework/src/mycontrol.cpp
HEADERS += \
../../../framework/inc/myapp.h \
../../../framework/inc/mycontrol.h
INCLUDEPATH += ../../../framework/extlibs
将此 .pri
包含在 MyApp.pro
中,例如:
Include this .pri
in MyApp.pro
like:
include(MyAppSrc.pri)
然后像主项目一样构建测试项目,在 MyAppUnitTest.pro
中添加一个额外的内容:
Then structure the testing project exactly like the main project, with one extra include in MyAppUnitTest.pro
:
include(MyAppUnitTestSrc.pri)
include(../MyApp/MyAppSrc.pri)
这篇关于如何在通过 QTestLib 对 Qt 应用程序进行单元测试时构建项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!