Qt Quick Test

介紹

Qt Quick Test 是用於 QML 應用程序的單元測試框架。測試用例被編寫成 JavaScript 函數在 TestCase 類型:

import QtQuick 2.3
import QtTest 1.0
TestCase {
    name: "MathTests"
    function test_math() {
        compare(2 + 2, 4, "2 + 2 = 4")
    }
    function test_fail() {
        compare(2 + 2, 5, "2 + 2 = 5")
    }
}
						

函數名稱開頭采用 test_ are treated as test cases to be executed. See the documentation for the TestCase and SignalSpy types for more information on writing test cases.

注意: There is no binary compatibility guarantee for the Qt Quick Test module. This means that an application that uses Qt Quick Test is only guaranteed to work with the Qt version it was developed against. However, source compatibility is guaranteed.

使用模塊

QML API

Qt Quick Test 中的 QML 類型是可用的透過 QtTest 導入。要使用類型,添加以下 import 語句到 .qml 文件:

import QtTest
						

C++ API

使用 C++ API 要求直接或透過其它依賴鏈接到模塊庫。一些構建工具為此有貢獻支持,包括 CMake and qmake .

構建采用 CMake

使用 find_package() 命令以在 Qt6 包中定位所需的模塊組件:

find_package(Qt6 REQUIRED COMPONENTS QuickTest)
target_link_libraries(mytarget PRIVATE Qt6::QuickTest)
						

另請參閱 構建采用 CMake 概述。

采用 qmake 構建

There are two ways to link against the corresponding C++ library. If your test project uses a QML TestCase , you should already have the following line in your project file:

CONFIG += qmltestcase
						

This will cause the test to link to the C++ QtQuickTest library.

If you have a C++-only test project, you can add the following line to your project file:

QT += qmltest
					
						

運行測試

Test cases are launched by a C++ harness that consists of the following code:

#include <QtQuickTest>
QUICK_TEST_MAIN(example)
						

Where "example" is the identifier to use to uniquely identify this set of tests.

Configure your CMakeLists.txt file and build your project using your favorite generator.

cmake_minimum_required(VERSION 3.2)
project(tst_example LANGUAGES CXX)
enable_testing()
find_package(Qt6 REQUIRED COMPONENTS QuickTest Qml)
#[[The test harness scans the specified source directory recursively
for "tst_*.qml" files. By default, it looks in the current directory,
which is usually where the executable is. This command makes it look
in the project's source directory instead.]]
add_definitions(-DQUICK_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
qt_standard_project_setup(REQUIRES 6.6)
add_executable(tst_example tst_example.cpp)
add_test(NAME tst_example COMMAND tst_example)
target_link_libraries(tst_example
    PRIVATE
    Qt6::QuickTest
    Qt6::Qml
)
						

添加 CONFIG += qmltestcase to your project file:

TEMPLATE = app
TARGET = tst_example
CONFIG += warn_on qmltestcase
SOURCES += tst_example.cpp
						

IMPORTPATH is specified in your .pro file, each import path added to IMPORTPATH will be passed as a command-line argument when the test is run using "make check":

IMPORTPATH += $$PWD/../imports/my_module1 $$PWD/../imports/my_module2
						

The test harness scans the specified source directory recursively for "tst_*.qml" files. If QUICK_TEST_SOURCE_DIR is not defined, then the current directory will be scanned when the harness is run. Other *.qml files may appear for auxillary QML components that are used by the test.

The -input command-line option can be set at runtime to run test cases from a different directory. This may be needed to run tests on a target device where the compiled-in directory name refers to a host. For example:

tst_example -input /mnt/SDCard/qmltests
						

It is also possible to run a single file using the -input 選項。例如:

tst_example -input data/test.qml
						
tst_example -input <full_path>/test.qml
						

注意: Specifying the full path to the qml test file is for example needed for shadow builds.

If your test case needs QML imports, then you can add them as -import options to the test program command-line.

The -functions command-line option will return a list of the current tests functions. It is possible to run a single test function using the name of the test function as an argument. For example:

tst_example Test_Name::function1
						

The -help command-line option will return all the options available.

tst_example -help
						

注意: Running a Qt Quick test case will always show a window on the screen, even if the test code doesn't involve any Quick UI. To avoid that, run the test executable with -platform offscreen .

在 QML 測試之前執行 C++

To execute C++ code before any of the QML tests are run, the QUICK_TEST_MAIN_WITH_SETUP macro can be used. This can be useful for setting context properties on the QML engine, amongst other things.

The macro is identical to QUICK_TEST_MAIN , except that it takes an additional type argument. The test framework will call slots and invokable functions with the following names:

名稱 目的 由於
void applicationAvailable() Called right after the QApplication object was instantiated. Use this function to perform setup that does not require a QQmlEngine 實例。 Qt 5.12
void qmlEngineAvailable(QQmlEngine *) Called when the QML engine is available. Any import paths , plugin paths ,和 extra file selectors will have been set on the engine by this point.

This function is called once for each QML test file, so any arguments are unique to that test. For example, this means that each QML test file will have its own QML engine.

此函數可用於 注冊 QML 類型 and 添加導入路徑 , amongst other things.

Qt 5.11
void cleanupTestCase() Called right after the test execution has finished. Use this function to clean up before everything will start to be destructed. Qt 5.12

The following example demonstrates how the macro can be used to set context properties on the QML engine:

// src_qmltest_qquicktest.cpp
#include <QtQuickTest>
#include <QQmlEngine>
#include <QQmlContext>
#include <QGuiApplication>
class Setup : public QObject
{
    Q_OBJECT
public:
    Setup() {}
public slots:
    void applicationAvailable()
    {
        // Initialization that only requires the QGuiApplication object to be available
    }
    void qmlEngineAvailable(QQmlEngine *engine)
    {
        // Initialization requiring the QQmlEngine to be constructed
        engine->rootContext()->setContextProperty("myContextProperty", QVariant(true));
    }
    void cleanupTestCase()
    {
        // Implement custom resource cleanup
    }
};
QUICK_TEST_MAIN_WITH_SETUP(mytest, Setup)
#include "src_qmltest_qquicktest.moc"
						

The .moc include is based on the file name of the .cpp file. For example, in the example above, the .cpp file is named src_qmltest_qquicktest.cpp . If the file was named MyTest.cpp , the include would be:

#include "MyTest.moc"
						

參考

許可

Qt Quick Test 在商業許可下是可用的來自 Qt 公司 。此外,它在自由軟件許可下也是可用的。從 Qt 5.4 起,這些自由軟件許可是 GNU LGPL (次一般公共許可) 第 3 版 ,或 GNU GPL (一般公共許可) 第 2 版 。見 Qt 許可 進一步瞭解細節。