Unittesting PyQt应用程序

时间:2015-12-02 14:29:32

标签: python unit-testing python-3.x pyqt5 python-unittest

我正在尝试测试我的PyQt应用。我需要在PyQt小部件中查看我的unittest的结果,但是当我运行unittesting时,应用程序关闭。

这是我的代码: 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)

我想查看QLabel中的测试结果。实施是真的吗?

1 个答案:

答案 0 :(得分:1)

这是一个非常晚的回应,但由于我遇到了同样的问题,我想我会发布。我认为解决方案是在主要调用中添加exit = False。

    unittest.main(module="validation_test", exit=False)