Use pytest-qt when pytesting PySdie and PyQt

Introduction

[This article is in the process of being written and will be updated! ]

Previously, in "Test Driven Development Startup with PySide & Pytest", I summarized the method of PySide + pytest, but I did the following Was there.

First, in conftest.py, register the following as fixture,

conftest.py


@pytest.fixture
def base_window():
    app = QApplication()
    mainWindow = QMainWindow()
    mainWindow.show()
    return mainWindow

In the test, use it to smoke test the widget

test_widget.py


def test_addWidget(base_widget):
    from MyCustomPySideLib,QCustomWidgets import QMyWidget
    tagWidget = QMyWidget()
    base_widget.setCentralWidget(tagWidget)

I was doing that, but I found out that some people seem to offer another method.

Introduction of pytest-qt

You can simulate this with pytest-qt. pytest-qt is treated as pytest plugins.

Installation can be done with pip.

pip install pytest-qt

Write using pytest-qt

When I rewrite the first test code using pytest-qt, it looks like this.

Additional Widget Test

test_widget.py


def test_addWidget(qtbot):
    from MyCustomPySideLib,QCustomWidgets import QMyWidget
    myWidget = QMyWidget()
    qtbot.addWidget(myWidget)

Recommended Posts

Use pytest-qt when pytesting PySdie and PyQt
[Python] Use and and or when creating variables
Use SQLAlchemy and multiprocessing