pytest-qt

4.5.0 · active · verified Sun Apr 12

pytest-qt is a pytest plugin that provides utilities and fixtures for testing PyQt and PySide applications. It simplifies GUI testing by offering tools to interact with widgets, handle signals, and manage the Qt event loop, ensuring reliable and robust tests for Qt-based UIs. The current version is 4.5.0, with minor releases arriving semi-regularly.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic test using the `qtbot` fixture. `qtbot` provides methods to interact with Qt widgets. `qtbot.addWidget` is used to manage the widget's lifecycle for the duration of the test, handling showing and cleanup. The specific `QtWidgets` import (e.g., PyQt5, PySide6) depends on your chosen Qt binding.

from PyQt5.QtWidgets import QLabel
import pytest

def test_label_text_using_qtbot(qtbot):
    # The QApplication instance is managed by pytest-qt (via the 'qapp' fixture if needed).
    # We typically don't create/quit QApplication explicitly in tests using pytest-qt.
    label = QLabel('Hello QtBot')
    qtbot.addWidget(label)
    # qtbot.addWidget ensures the widget is shown and its lifecycle is managed for the test.
    # No explicit label.show(), qtbot.waitExposed(), or label.close() needed for basic interaction.
    assert label.text() == 'Hello QtBot'

view raw JSON →