Stefan
Stefan

Reputation: 211

How to test QtQuick Qml in Python

I'm using PyQt to integrate QtQuick into my Python app. I'm trying to figure out the best way to test my QtQuick Qml pages within a Python unittest framework. I'd like to pass a list of Qml files to some test function that makes sure no errors/exceptions exist for any of these files. Currently I'm trying to load a page into QQmlComponent and check for errors, but I haven't been able to get this working:

def test_qml(self):
    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()
    rel = "/gui/QT/Page1.qml"
    c = QQmlComponent(engine, QUrl.fromLocalFile(SRC_PATH + os.path.normpath(rel)))
    print(c.errors())

Moreover from what I've read I think to get show errors with QQmlComponent I should catch a signal onStatusChange and then check so this seems like the wrong approach for me. What is the best way to go about trying to test qml pages in Python?

Upvotes: 1

Views: 696

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

From what I understand you, you want to make the error message more readable. the errors() method returns a list of QQmlError, and this class has methods that give us accurate error information:

import sys

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtQml import *

type_error_toString = { 
    QtDebugMsg: "debug", 
    QtInfoMsg : "info", 
    QtWarningMsg : "wargning",
    QtCriticalMsg: "critical",
    QtFatalMsg: "fatal"
}

app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
path = "/path/of/item.qml"
c = QQmlComponent(engine, QUrl.fromLocalFile(path))

if c.isError():
    for error in c.errors():
        print(error.toString())
        print("type: {}, row : {}, column: {}, message: {}"
            .format(
                type_error_toString[error.messageType()],
                error.line(), 
                error.column(),
                error.description())
            )

Upvotes: 2

Related Questions