pytest-dev / pytest-dev/pytest-qt

Proposal: add `qmlloader` fixture

Open
#474 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement :cool:
Dominant language
Python
Stars
455
Forks
72
Avg merge
12h 26m
Merged PRs (30d)
1

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the proposed QmlTestCase and qmlloader fixture, then inspect qmltester.qml and the example test_accessible_from_qml usage to determine where this fixture would belong. Confirm the supported load, loads, and find behavior and agree on the project’s expected tests and API before implementing; done means the contribution is accepted with those behaviors covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.