Skip to content

Instantly share code, notes, and snippets.

@mitchcurtis
Last active September 5, 2022 14:03
Show Gist options
  • Save mitchcurtis/6b8a3e572db729309d583f086e3330c3 to your computer and use it in GitHub Desktop.
Save mitchcurtis/6b8a3e572db729309d583f086e3330c3 to your computer and use it in GitHub Desktop.
Qt Quick Auto Test (C++)
cmake_minimum_required(VERSION 3.5)
project(tst_untitled LANGUAGES CXX)
enable_testing()
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Gui Test Quick)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Gui Test Quick)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(tst_untitled tst_untitled.cpp)
add_test(NAME tst_untitled COMMAND tst_untitled)
target_link_libraries(tst_untitled
PRIVATE
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Test
Qt${QT_VERSION_MAJOR}::Quick
)
#include <QtTest>
#include <QQmlComponent>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
class tst_untitled : public QObject
{
Q_OBJECT
public:
tst_untitled();
private Q_SLOTS:
void testCase1();
};
tst_untitled::tst_untitled()
{
}
void tst_untitled::testCase1()
{
QQmlApplicationEngine engine;
QQmlComponent component(&engine);
const QString testFilePath = QFINDTESTDATA("data/window.qml");
component.loadUrl(QUrl::fromLocalFile(testFilePath));
QVERIFY2(component.isReady(), qPrintable(QString::fromUtf8(
"Failed to load component at %1:\n%2").arg(testFilePath, component.errorString())));
const QVariantMap initialProperties;// = {{ "foo", "bar" }, { "blah", "blah" }}
QScopedPointer<QObject> rootObject(component.createWithInitialProperties(initialProperties));
QVERIFY2(rootObject, qPrintable(QString::fromUtf8("Failed to create window: %1").arg(component.errorString())));
auto window = qobject_cast<QQuickWindow*>(rootObject.data());
QVERIFY2(window, "Root object must be a QQuickWindow subclass");
// Add stuff here
}
QTEST_MAIN(tst_untitled)
#include "tst_untitled.moc"
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: "tst_untitled"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment