集成测试:使 C++ 组件对 qmltestrunner 可见
Integration testing: making C++ components visible to `qmltestrunner`
假设我有许多 QML 组件。
他们使用组件或 QObject
(假设它是一个数据源,但可以是任何东西,即使是旧的 Horse
/Animal
/Dog
我用 C++ 编写的方法 bark()
) 并以某种方式在 main.cpp
中公开。
虽然 proper 单元测试当然会对其进行存根,但我可能想编写一个 集成测试 以查看它们一起玩得开心
然后如何让它们对 qmltestrunner
可见?
如果根本不可能,QML 和 C++ 组件的最佳集成测试方法是什么?
我已经解决了集成测试的问题(除了你描述的问题之外,对我来说还有 QML 文件需要资源文件中的东西的问题,如果我需要组件,这些东西将找不到通过基于文件的访问,就像 qmltestrunner
所做的那样)通过在我的 main.cpp
中有一个标志 TEST_RUNNER
来确定是否应该启动常规应用程序或者是否应该启动 quick_test_main
(quick_test_main
是编程可访问的核心或 qmltestrunner
)。看起来如下:
// define to enable the test harness
#define TEST_RUNNER
#ifdef TEST_RUNNER
#include <QtQuickTest/quicktest.h>
#endif
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// register components here as you're already doing
#ifndef TEST_RUNNER
// not defined: regular application start
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
return app.exec();
#else
// adapt path accordingly
return quick_test_main(argc, argv, "MyTests", "../my_qmltests_path/");
#endif
}
测试用例如下所示(请注意,我正在通过资源路径加载组件,因为这就是我的应用程序的工作方式;通过文件加载将无法加载 QML 文件所需的资源...) :
import QtQuick 2.0
import QtTest 1.0
TestCase {
name: "MainMenu"
Loader {
id: main_loader
source: "qrc:/qml/main.qml"
}
function test_on_off(){
wait(3000);
}
}
假设我有许多 QML 组件。
他们使用组件或 QObject
(假设它是一个数据源,但可以是任何东西,即使是旧的 Horse
/Animal
/Dog
我用 C++ 编写的方法 bark()
) 并以某种方式在 main.cpp
中公开。
虽然 proper 单元测试当然会对其进行存根,但我可能想编写一个 集成测试 以查看它们一起玩得开心
然后如何让它们对 qmltestrunner
可见?
如果根本不可能,QML 和 C++ 组件的最佳集成测试方法是什么?
我已经解决了集成测试的问题(除了你描述的问题之外,对我来说还有 QML 文件需要资源文件中的东西的问题,如果我需要组件,这些东西将找不到通过基于文件的访问,就像 qmltestrunner
所做的那样)通过在我的 main.cpp
中有一个标志 TEST_RUNNER
来确定是否应该启动常规应用程序或者是否应该启动 quick_test_main
(quick_test_main
是编程可访问的核心或 qmltestrunner
)。看起来如下:
// define to enable the test harness
#define TEST_RUNNER
#ifdef TEST_RUNNER
#include <QtQuickTest/quicktest.h>
#endif
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// register components here as you're already doing
#ifndef TEST_RUNNER
// not defined: regular application start
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
return app.exec();
#else
// adapt path accordingly
return quick_test_main(argc, argv, "MyTests", "../my_qmltests_path/");
#endif
}
测试用例如下所示(请注意,我正在通过资源路径加载组件,因为这就是我的应用程序的工作方式;通过文件加载将无法加载 QML 文件所需的资源...) :
import QtQuick 2.0
import QtTest 1.0
TestCase {
name: "MainMenu"
Loader {
id: main_loader
source: "qrc:/qml/main.qml"
}
function test_on_off(){
wait(3000);
}
}