Skip to content

Proposal: add qmlloader fixture #474

Open
@nrbnlulu

Description

Hi I created this nice fixture and wanted to ask if it would be usefull to contribute it here?
It supports loading QML from strings or from file and retrieving the loaded item with ease.

T = TypeVar("T")

@define(slots=False)
class QmlTestCase:
    bot: QtBot
    engine: QQmlApplicationEngine = field(factory=QQmlApplicationEngine)

    def __attrs_post_init__(self):
        main = Path(__file__).parent / "qmltester.qml"
        self.engine.load(main.resolve(True))

    @property
    def _loader(self) -> QQuickItem:
        self.root = self.engine.rootObjects()[0]
        return self.root.findChild(QQuickItem, "contentloader")

    def load(self, path: Path) -> QQuickItem:
        self.bot.wait(100)
        self._loader.setProperty("source", str(path.resolve(True)))
        return self._loader.property("item")

    def loads(self, content: str) -> QQuickItem:
        self.comp = QQmlComponent(self.engine)
        self.comp.setData(content.encode("utf-8"), QUrl())
        self._loader.setProperty("source", "")
        self._loader.setProperty("sourceComponent", self.comp)
        return self._loader.property("item")

    def find(self, objectname: str, type: T = QQuickItem) -> T:
        return self.window.findChild(type, objectname)


@pytest.fixture()
def qmlloader(qtbot):
    return QmlTestCase(qtbot)

Usage:

@pytest.mark.parametrize("status", iter(object_with_enum.Status))
def test_accessible_from_qml(qmlloader, status):
    qml = (
        """
import QtQuick
import QtGql 1.0 as GQL

Rectangle {
    property int enumValue: GQL.Enums.%s
}
"""
        % status.name
    )

    EnumTestCase.compile()
    item = qmlloader.loads(qml)
    assert item.property("enumValue") == status.value

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions