I am trying to test my QtApplication using pytest-qt and need some assistance with the basics.
Give a simple mwe such as this:
# mwe.py
import sys
from PyQt5.QtWidgets import (QWidget, QToolTip,
QPushButton, QApplication)
from PyQt5.QtGui import QFont
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.count = 0
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50, 50)
btn.clicked.connect(self.pushedTheButton)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Tooltips')
self.show()
def pushedTheButton(self):
print("Button pressed")
self.count += 1
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec())
if __name__ == '__main__':
main()
How would I test that the counter has incremented when the button is pressed?
from mwe import Example
import pytest
import pytestqt
from pytestqt.qtbot import QtBot
import sys
from PyQt5.Qt import QApplication
def test_press_button(qtbot: QtBot):
app = QApplication(sys.argv)
ex = Example()
qtbot.add_widget(ex)
sys.exit(app.exec())
# how to press the button and verify that counter is incermented?
Note, I've been able to do this outside the main loop, by just instantiating the Example
but for my integration testing I want to have the full application open and running.
Thanks in advance!