利用QTestLib对Qt应用进行单元测试时如何构造项目
我得到了我的Qt项目,我正在使用Qt Creator。我想对所有代码进行单元测试。利用QTestLib对Qt应用进行单元测试时如何构造项目,qt,unit-testing,qtestlib,qttest,Qt,Unit Testing,Qtestlib,Qttest,我得到了我的Qt项目,我正在使用Qt Creator。我想对所有代码进行单元测试。 然而,我对QTestLib框架很陌生,但每个人都推荐它用于测试基于Qt的源代码。现在我有点困惑如何用app项目来构造测试项目 我可以把所有源代码和测试代码放在同一个项目中吗?如果是这样,我如何管理它们?我没有找到任何选项可以让我在一个项目中启动应用程序或启动测试 如果我将应用程序源代码和测试代码放在不同的项目中,测试项目将引用应用程序项目,这不是很方便 对于需要测试的类,如何管理测试代码 在这种情况下,你们是如何
然而,我对QTestLib框架很陌生,但每个人都推荐它用于测试基于Qt的源代码。现在我有点困惑如何用app项目来构造测试项目
在这种情况下,你们是如何管理测试代码的?谢谢。我使用CMake的Qt Creator而不是qmake来构建我的Qt项目 基本上,我必须:
src
tests
每个测试本身就是一个测试类的程序要测试的应用程序被编译为库。。将文件夹src中的所有源代码编译为库
// ClassTest.cpp
#include "ClassTest.h"
#include "Class2Test.h" // Class of the app
#include <QtTest/QtTest>
ClassTest::ClassTest( QObject* parent )
: QObject(parent)
{
}
QTEST_MAIN( ClassTest )
#include "ClassTest.moc"
在测试文件夹CMakeLists.txt示例中,针对每个测试
qt4_automoc( ${test_src} )
add_executable( ${test_name} ${test_src} )
target_link_libraries( ${test_name}
MyAPP
${QT_LIBRARIES}
${QT_QTTEST_LIBRARY}
)
它仍然在同一个项目中,但您可以添加一个标志,让用户编译测试或不编译测试。它是干净的,因为应用程序保持不变,它允许您测试应用程序的每个类。第一个结构源,如下所示:
MyApp
MyAppUnitTest
在MyApp
项目下,使用MyAppSrc.pri
查找源文件:
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(MyAppSrc.pri)
然后像主项目一样构造测试项目,在MyAppUnitTest.pro
中包含一个额外的include:
include(MyAppUnitTestSrc.pri)
include(../MyApp/MyAppSrc.pri)
我使用这种方法:
也就是说,在构建所有内容的单个.pro
文件中放置一个test
config。文件层次结构:
myproject.pro
src/
Example1.cpp
Example2.cpp
Example1.h
Example2.h
test/
ExampleTest.cpp
ExampleTest.h
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)
}
在我的示例中,我正在构建一个插件库,但该方法也适用于应用程序。对于应用程序,很可能在else
子句下需要SOURCES-=src/main.cpp
,插件库没有它。如果不这样做,应用程序的main()
将与单元测试的main()
冲突
ExampleTest.cpp
如下所示:
#include "ExampleTest.h"
void ExampleTest::exampleTest(){
//Do the tests
}
QTEST_MAIN(ExampleTest)
#include <QtTest/QtTest>
class ExampleTest : public QObject {
Q_OBJECT
private slots:
void exampleTest();
};
ExampleTest.h
如下所示:
#include "ExampleTest.h"
void ExampleTest::exampleTest(){
//Do the tests
}
QTEST_MAIN(ExampleTest)
#include <QtTest/QtTest>
class ExampleTest : public QObject {
Q_OBJECT
private slots:
void exampleTest();
};
我喜欢其他答案,但我也想给出一些反馈,说明我们在我目前工作的公司是如何做到这一点的:
subdirs
项目(这将是一个顶级项目,它将管理所有项目,包括您的库项目或任何您想要测试的项目)
+-----MyProject (top-level subdirs)
|
+-----Library (library project, UI project etc.)
子项目
项目(用于测试)
QUnitTest
项目,并将其添加到测试子项目中
+-----MyProject (subdirs)
|
+-----Library (library project, UI project etc.)
|
+-----Tests (subdirs for tests)
|
+----- TestA (QUnitTest project for testing feature A)
...
|
+-----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)
subdirs
<代码>子目录还可以确保在文件系统中创建真实的目录。如果要避免过多的子文件夹
ing,可以将测试分组到测试
项目文件夹内文件系统中自己创建的文件夹中
除此之外,我还建议为模板项目添加子目录
+-----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)
这当然是基于您的库的功能。对于模板项目,我指的是自定义小部件等,它们链接到您的库,并以用户应该看到的方式选择性地(或全部)公开其功能。例如,如果您有一个管理各种相机设备的库,那么您可以为每个相机设备创建一个模板项目,从而允许库的用户只需复制粘贴特定的模板项目并展开它,或者至少了解库的集成通常是如何进行的。这可以减少文档编制,同时提供良好的自包含示例,这些示例可以减少开发时间,而开发时间本来是花在弄清楚库的集成和使用是如何工作的(可以说这是一组Hello World项目:))。最后但并非最不重要的一点是,您可以概述不同用例的解决方案。fifth。。你能接受更多的答案吗?当然@在UmNyobe中,我最终在不同的项目中部署了源代码和测试代码。测试项目通过pri文件引用了具有相对路径的相同源。如果您清楚地描述了您所做的工作,您将获得我的赏金:D@UmNyobe,很抱歉回复晚了,请查看我下面的帖子。由于命名原因,有点难以阅读。什么是testclass,什么是要测试的class?使用这种方法,源代码是否编译了两次(一次用于MyApp,一次用于MyAppUnitTest)?那么,我如何使qmake落入test{Configuring test build…}…
case?
...
|
+-----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)
+-----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)