fif*_*fth 36 qt unit-testing qttest qtestlib
我得到了我的Qt项目,我正在使用Qt Creator.我想对我的所有代码进行单元测试.
但是我在QTestLib框架上相当新,但是每个人都推荐它用于测试基于Qt的源代码.现在我有点困惑如何用app项目构建测试项目.
在这种情况下,你们如何管理测试代码?谢谢.
fif*_*fth 16
第一个结构来源如下:
MyApp
MyAppUnitTest
Run Code Online (Sandbox Code Playgroud)
在MyApp
项目下,使用a MyAppSrc.pri
来查找源文件:
SOURCES += \
../../../framework/src/myapp.cpp \
../../../framework/src/mycontrol.cpp
HEADERS += \
../../../framework/inc/myapp.h \
../../../framework/inc/mycontrol.h
INCLUDEPATH += ../../../framework/extlibs
Run Code Online (Sandbox Code Playgroud)
这包括.pri
在MyApp.pro
像:
include(MyAppSrc.pri)
Run Code Online (Sandbox Code Playgroud)
然后完全像主项目一样构建测试项目,另外一个包括MyAppUnitTest.pro
:
include(MyAppUnitTestSrc.pri)
include(../MyApp/MyAppSrc.pri)
Run Code Online (Sandbox Code Playgroud)
Ayb*_*gür 10
我使用这种方法:http://xilexio.org/?p = 125
即,将test
配置放在.pro
构建所有内容的单个文件中.文件层次:
myproject.pro
src/
Example1.cpp
Example2.cpp
Example1.h
Example2.h
test/
ExampleTest.cpp
ExampleTest.h
Run Code Online (Sandbox Code Playgroud)
myproject.pro
文件:
QT += #needed modules
CONFIG += qt c++11
HEADERS += \
src/Example1.h \
src/Example2.h
SOURCES += \
src/Example1.h \
src/Example2.h
test{
message(Configuring test build...)
TEMPLATE = app
TARGET = myapptests
QT += testlib
HEADERS += \
test/ExampleTest.h
SOURCES += \
test/ExampleTest.cpp
}
else{
TEMPLATE = lib
TARGET = myapp
CONFIG += plugin
TARGET = $$qtLibraryTarget($$TARGET)
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,我正在构建一个插件库,但该方法也适用于应用程序.在应用程序的情况下,很可能SOURCES -= src/main.cpp
在该else
子句下需要,插件库没有它.如果没有这样做,main()
应用程序将与main()
单元测试冲突.
ExampleTest.cpp
看起来如下:
#include "ExampleTest.h"
void ExampleTest::exampleTest(){
//Do the tests
}
QTEST_MAIN(ExampleTest)
Run Code Online (Sandbox Code Playgroud)
ExampleTest.h
看起来如下:
#include <QtTest/QtTest>
class ExampleTest : public QObject {
Q_OBJECT
private slots:
void exampleTest();
};
Run Code Online (Sandbox Code Playgroud)
要构建项目测试,请在常规构建的单独目录中运行:
qmake path/to/myproject.pro "CONFIG += test"
Run Code Online (Sandbox Code Playgroud)
我喜欢其他答案,但我也想在我目前工作的公司提供一些反馈:
创建一个subdirs
项目(这将是管理所有项目的顶级项目,包括您的库项目或任何您想要测试的项目)
+-----MyProject (top-level subdirs)
Run Code Online (Sandbox Code Playgroud)将库项目添加为子项目
+-----MyProject (top-level subdirs)
|
+-----Library (library project, UI project etc.)
Run Code Online (Sandbox Code Playgroud)添加其他subdirs
项目(用于测试)
+-----MyProject (top-level subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
Run Code Online (Sandbox Code Playgroud)创建一个QUnitTest
项目并将其添加到测试subdirs
项目中
+-----MyProject (subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
|
+----- TestA (QUnitTest project for testing feature A)
Run Code Online (Sandbox Code Playgroud)添加尽可能多的测试
...
|
+-----Tests (subdirs for test)
|
+----- TestA (QUnitTest project for testing feature A)
|
+----- TestB (QUnitTest project for testing feature B)
|
+----- TestC (QUnitTest project for testing feature C)
|
...
|
+----- TestZ (QUnitTest project for testing feature Z)
Run Code Online (Sandbox Code Playgroud)如果您需要在组中进行分组测试,您也可以使用它subdirs
来执行此操作.subdirs
还确保在文件系统中创建真实目录.如果您想避免过多的操作subdirs
,可以将测试分组到您在Tests
项目文件夹内的文件系统中自己创建的文件夹中.
除此之外,我还建议添加一个subdirs
for 模板项目.
+-----MyProject (subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
| |
| ...
|
+-----Templates (subdirs for template projects
|
+----- TemplateA (template project for feature A)
|
+----- TemplateB (template project for feature B)
|
+----- TemplateAB (template project for feature A and B together)
|
...
|
+----- TemplateZ (template project for feature Z)
Run Code Online (Sandbox Code Playgroud)
这当然基于您的库的功能.对于模板项目,我指的是自定义小部件等,它们链接到您的库,并以其应该向用户显示的方式有选择地(或全部)公开其功能.例如,如果您有一个管理各种相机设备的库,您可以为每个相机设备创建一个模板项目,从而允许您的库的用户只需复制粘贴特定的模板项目并展开它,或者至少看看如何集成你的图书馆一般应该发生.这样可以减少文档,同时提供很好的自包含示例,这样可以减少开发时间,而这些开发时间用于确定库的集成和使用方式是如何工作的(你可以说它是一组Hello World项目:)).最后但并非最不重要的是,您可以概述不同用例的解决方案.