1

I am trying to test my PyQt app. I need to view results of my unittest in PyQt widget but when I run unittesting the app closes.

This is my code: validation_test_app.py:

from PyQt5 import QtCore, QtWidgets
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
import sys
import unittest

def get_size(url):
    manager = QNetworkAccessManager()
    response = manager.get(QNetworkRequest(QtCore.QUrl(r"https://" + url)))
    event = QtCore.QEventLoop()
    response.finished.connect(event.quit)
    event.exec()
    html = response.readAll()
    size = html.size()
    return size

class MainWindow(QtWidgets.QWidget):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__()

        self.layout = QtWidgets.QVBoxLayout()
        self.label = QtWidgets.QLabel("Checking tests...")

        self.btn = QtWidgets.QPushButton("push")
        self.btn.clicked.connect(self.btn_clicked)

        self.layout.addWidget(self.label, alignment=QtCore.Qt.AlignCenter)
        self.layout.addWidget(self.btn)
        self.setLayout(self.layout)

    def btn_clicked(self):
        unittest.main(module="validation_test")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    main_window = MainWindow()
    main_window.show()

    sys.exit(app.exec_())

validation_test.py:

import unittest
from validation_test_app import get_size

class TestUrls(unittest.TestCase):

    def test_1(self):
        size = get_size("apps4all.ru")
        self.assertEqual(size, 0)

    def test_2(self):
        size = get_size("google.com")
        self.assertNotEqual(size, 0)

I want to view results of testing in QLabel, for example. Is it real to implement ?

Oleg Klimenko
  • 138
  • 1
  • 5
  • 20
  • Do you mean getting the result of `unit-testing` on a `QLabel` widget on your PyQt App ? – Iron Fist Dec 02 '15 at 14:37
  • @IronFirst my app starts, do unittesting, and set text of QLabel, the result, for example passed or failed testing. p.s. I've tried to output result to the file and then read it. But app closes. ((( – Oleg Klimenko Dec 02 '15 at 14:47
  • Do you get any traceback error message?..if so, please post it here.. – Iron Fist Dec 02 '15 at 14:56
  • @IronFirst Just a standard like in unittesting: .. ---------------------------------------------------------------------- Ran 2 tests in 0.510s OK – Oleg Klimenko Dec 02 '15 at 14:58
  • @ekhumoro ...where is the link to the Duplicate question? – Iron Fist Dec 02 '15 at 18:52
  • @ekhumoro that topic didn't solve my problem, because I need to view the results of testing my app in the app. When I use unittesting app closes, so that topic is not a solution for me... – Oleg Klimenko Dec 03 '15 at 09:48

1 Answers1

1

This is a very late response, but since I had the same problem I figured I'd post. I think the solution is to add exit=False to the main call.

unittest.main(module="validation_test", exit=False)
ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
shao.lo
  • 4,387
  • 2
  • 33
  • 47