我正在为新项目进行正确的设置。我正在尝试为C ++和QML元素合并单元测试,但是我真的停留在配置QML部分上。
我已经阅读了Qt Quick Test文档,但是它并没有帮助我了解其中包含测试项目的subdirs项目的基础知识。首先,我读到我的主应用程序和测试应用程序的源代码都应该有单独的项目。
我应该如何将Qt Quick组件从主应用程序导入到测试应用程序的 tst_unittest.qml文件中,以便可以测试其行为?
我的意思是这样的:
// tst_unittest.qml
import QtQuick 2.0
import QtTest 1.0
//some kind of import here?
TestCase {
name: "UnitTests"
ComponentToTest{
id: entityToTest
}
function test_case() {
compare(entityToTest.funcToTest(), false, "funcToTest unit test")
}
}
我的项目树如下所示:
-subdirs_project
|---subdirs_project.pro
|
|---main_app
| |---main_app.pro
| |---ComponentToTest.qml
|
|---test_app
| |---test_app.pro
| |---tst_unittests.qml
和我的test_app.pro:
CONFIG += warn_on qmltestcase
TEMPLATE = app
DISTFILES += \
tst_unittests.qml
SOURCES += \
main.cpp
IMPORTPATH += \
$$PWD/../main_app/ComponentToTest.qml
这至少是应该看起来的样子附近的地方吗?还是我应该以其他方式将其丢弃?
我已经设法配置了项目并运行了所有测试,所以我认为最好在这里为所有可能需要此工具的人们写这篇文章。我不会在这里粘贴每个文件的内容,但是我将发布最重要的文件,这些文件给我带来了一些麻烦。例如,testentity.cpp
并且testentity.h
只是普通源文件和头一个简单的C ++类的文件,我不认为解释QML组件这里需要。如果下面没有描述任何文件,您可以从一开始就认为它还可以,并且Qt Creator自行完成了所有操作,或者所做的更改很小。
文件组织:
.
├── ExampleProject
│ ├── ExampleProject.pro
│ ├── controls
│ │ ├── another_module
│ │ │ ├── testentity.cpp
│ │ │ └── testentity.h
│ │ ├── example_module
│ │ │ ├── testclass.cpp
│ │ │ ├── testclass.h
│ │ │ ├── testsubject.cpp
│ │ │ └── testsubject.h
│ │ ├── components
│ │ │ ├── applayout
│ │ │ │ ├── LayoutComponent.qml
│ │ │ │ └── SideMenu.qml
│ │ │ └── controls
│ │ │ └── PositioningDummy.qml
│ │ ├── controls.pro
│ │ ├── main.cpp
│ │ ├── main.qml
│ │ └── qml.qrc
│ ├── tests_cpp
│ │ ├── main.cpp
│ │ ├── tests_cpp.pro
│ │ ├── tst_controls.cpp
│ │ ├── tst_controls.h
│ │ ├── tst_layout.cpp
│ │ └── tst_layout.h
│ └── tests_qt_quick
│ ├── main.cpp
│ ├── tests_qt_quick.pro
│ ├── tst_controls.qml
│ └── tst_layout.qml
└── README.md
mainprojectfile.pro
TEMPLATE = subdirs
SUBDIRS += \
controls \
tests_cpp \
tests_qt_quick
tests_cpp.pro
QT += testlib
QT -= gui
CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += tst_controls.cpp \
main.cpp \
tst_layout.cpp
HEADERS += \
tst_controls.h \
tst_layout.h
# 1) Define each module as INCDIR_MODULE_NAME
# 2) Add INCDIR_MODULE_NAME to INCLUDEPATH
# 3) Add each of the classes you want to test into HEADERS and SOURCES
INCDIR_EXAMPLE_MODULE = ../controls/example_module
INCDIR_ANOTHER_MODULE = ../controls/another_module
INCLUDEPATH += \
$$INCDIR_EXAMPLE_MODULE \
$$INCDIR_ANOTHER_MODULE
SOURCES += \
$$INCDIR_EXAMPLE_MODULE/testclass.cpp \
$$INCDIR_EXAMPLE_MODULE/testsubject.cpp \
$$INCDIR_ANOTHER_MODULE/testentity.cpp \
HEADERS += \
$$INCDIR_EXAMPLE_MODULE/testclass.h \
$$INCDIR_EXAMPLE_MODULE/testsubject.h \
$$INCDIR_ANOTHER_MODULE/testentity.h
tests_cpp / main.cpp
#include <QTest>
#include "tst_layout.h"
#include "tst_controls.h"
int main(int argc, char *argv[])
{
int status = 0;
Tst_Layout tst_layout;
status |= QTest::qExec(&tst_layout, argc, argv);
Tst_Controls tst_controls;
status |= QTest::qExec(&tst_controls, argc, argv);
return status;
}
tests_cpp / tst_layout.h
#ifndef TST_LAYOUT_H
#define TST_LAYOUT_H
#include <QtTest>
// include class from our controls project
#include "testentity.h"
class Tst_Layout : public QObject
{
Q_OBJECT
public:
explicit Tst_Layout(QObject *parent = nullptr);
private slots:
void initTestCase();
void cleanupTestCase();
void test_case1();
};
#endif
tests_qt_quick.pro
CONFIG += warn_on qmltestcase
TEMPLATE = app
DISTFILES += \
tst_controls.qml \
tst_layout.qml
SOURCES += \
main.cpp
tests_qt_quick / tst_layout.qml
import QtQuick 2.12
import QtTest 1.12
import "../controls/components/applayout" as AppLayout
import "../controls/components/controls" as Controls
TestCase {
name: "Layout"
function initTestCase() {}
function cleanupTestCase() {}
// please bear in mind that test functions below are not my actual tests
// they are here only to show the very basics of how to set up Qt Quick tests
function test_layoutEntityReturnTrue() {
verify(layoutEntity.returnTrue(),("Put some test fail message here."))
}
function test_sideMenuReturnTrue() {
verify(layoutEntity.returnTrue(),("Put some test fail message here."))
}
AppLayout.LayoutComponent {
id: layoutEntity
}
AppLayout.SideMenu {
id: sideMenuEntity
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句