pyqt如何使用unittest来检查登录名,密码和结果?

时间:2018-08-24 04:19:06

标签: python unit-testing pyqt pyqt5

我的错误消息是

  

AttributeError:类型对象'MainWindow'没有属性   'nameLineEdit'

我尝试使用pyqt unittest检查登录名和密码(因为我的项目是pyqt5)

但是许多单元测试不是针对gui python项目

所以我在Google上搜索了一个教程(该教程适用于pyqt4,但我需要使用pyqt5) http://johnnado.com/pyqt-qtest-example/

以下是我的项目源代码 https://drive.google.com/file/d/1gxVzY2yg83_y5OmHYUprsljMoP5xhbLT/view?usp=sharing

然后我记录我的屏幕 https://youtu.be/yiMUGphm6fk 关于项目

我简单的pyqt5项目,练习unittest以检查登录状态 我遵循pyqt4项目教程,但无法正常工作,该项目包含三个脚本

第一个脚本(main.py)

# -*- coding: utf-8 -*-

"""
Module implementing MainWindow.
"""
import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication

from Ui_main import Ui_MainWindow


class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

    @pyqtSlot()
    def on_loginButton_clicked(self):
        if self.nameLineEdit.text()=='admin' and self.passwordLineEdit.text()=='123456':
            self.loginStatusLabel.setText('Success Login')
        else:
            self.loginStatusLabel.setText('Failed Login')

if __name__=="__main__":
    app=QApplication(sys.argv)
    Main=MainWindow()
    Main.show()
    sys.exit(app.exec_())

第二个脚本(test_login.py)

import sys
import unittest
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtTest import QTest

import main

app = QApplication(sys.argv)


class test01(unittest.TestCase):
    '''Test the margarita mixer GUI'''

    def setUp(self):
        print('inital ok')

    def test_defaults(self):

        self.nameLineEdit=main.MainWindow.nameLineEdit
        self.passwordLineEdit=main.MainWindow.passwordLineEdit
        loginButton = main.MainWindow.loginButton

        self.assertEqual(self.nameLineEdit.text(), 'admin')
        self.assertEqual(self.passwordLineEdit.text(), '123456')


        QTest.mouseClick(loginButton, QtCore.Qt.LeftButton)

if __name__ == "__main__":
    unittest.main()

第三个脚本(Ui_main)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'F:\project\pyqt_unitest01\main.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(302, 157)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.loginButton = QtWidgets.QPushButton(self.centralWidget)
        self.loginButton.setGeometry(QtCore.QRect(200, 100, 81, 41))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.loginButton.setFont(font)
        self.loginButton.setObjectName("loginButton")
        self.nameLineEdit = QtWidgets.QLineEdit(self.centralWidget)
        self.nameLineEdit.setGeometry(QtCore.QRect(110, 10, 181, 31))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.nameLineEdit.setFont(font)
        self.nameLineEdit.setObjectName("nameLineEdit")
        self.passwordLineEdit = QtWidgets.QLineEdit(self.centralWidget)
        self.passwordLineEdit.setGeometry(QtCore.QRect(110, 60, 181, 31))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.passwordLineEdit.setFont(font)
        self.passwordLineEdit.setObjectName("passwordLineEdit")
        self.nameLabel = QtWidgets.QLabel(self.centralWidget)
        self.nameLabel.setGeometry(QtCore.QRect(20, 10, 81, 31))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.nameLabel.setFont(font)
        self.nameLabel.setObjectName("nameLabel")
        self.passwordLabel = QtWidgets.QLabel(self.centralWidget)
        self.passwordLabel.setGeometry(QtCore.QRect(20, 60, 81, 31))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.passwordLabel.setFont(font)
        self.passwordLabel.setObjectName("passwordLabel")
        self.loginStatusLabel = QtWidgets.QLabel(self.centralWidget)
        self.loginStatusLabel.setGeometry(QtCore.QRect(20, 105, 141, 31))
        font = QtGui.QFont()
        font.setFamily("Agency FB")
        font.setPointSize(16)
        self.loginStatusLabel.setFont(font)
        self.loginStatusLabel.setObjectName("loginStatusLabel")
        MainWindow.setCentralWidget(self.centralWidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.loginButton.setText(_translate("MainWindow", "login"))
        self.nameLabel.setText(_translate("MainWindow", "name"))
        self.passwordLabel.setText(_translate("MainWindow", "password"))
        self.loginStatusLabel.setText(_translate("MainWindow", "login_status"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

如何正确使用unittest测试pyqt5项目(或任何更好的主意或框架) 谢谢大家!

1 个答案:

答案 0 :(得分:0)

您显示的问题是对OOP的了解很少,这些类是抽象或模型,它们仅用于创建对象,而这是您尚未做的事情,因此解决方案的一部分是创建一个对象MainWindow类。

另一方面,已进行测试以验证功能是否正确,在您的情况下,无法在QLineEdit中进行验证,因为要输入文本,您必须将其放置在正确的位置,以进行验证loginStatusLabel是因为GUI是可以控制它的控件,也就是说,用户放置用户名和密码,单击并因此必须更改指示的标签。

import sys
import unittest
from PyQt5 import QtCore, QtWidgets, QtTest
from main import MainWindow

app = QtWidgets.QApplication(sys.argv)

class test01(unittest.TestCase):
    '''Test the margarita mixer GUI'''
    def setUp(self):
        self.form = MainWindow()

    def test_defaults(self):
        self.form.nameLineEdit.setText('admin')
        self.form.passwordLineEdit.setText('123456')
        QtTest.QTest.mouseClick(self.form.loginButton, QtCore.Qt.LeftButton)
        self.assertEqual(self.form.loginStatusLabel.text(), 'Success Login')

if __name__ == "__main__":
    unittest.main()