PyQt5 + PyCharm,测试没有运行并导致分段错误+崩溃

时间:2017-07-25 07:17:21

标签: python pycharm pyqt5 sigsegv

我已将此错误缩小到此错误消息:

ASSERT: "qApp" in file /usr/include/qt/QtTest/qtestkeyboard.h, line 192

有没有人对这意味着什么有所了解?

如果我将所有测试都放在setUp()以下的单个函数中,那么它可以正常工作。这是为什么?

每当我在PyQt测试中与GUI元素进行交互(例如设置旋转框的值)时,我会遇到分段错误,我正在努力弄清楚原因。

我创建了一些简单的测试来查看这种行为是否可重复:

class TestMainWindow(TestCase):
    def setUp(self):   
        testApp = QtWidgets.QApplication(sys.argv)   
        dialog = QtWidgets.QMainWindow()   
        self.prog = application.mainWindow(dialog)   
    def test_update(self):   
        self.assertEqual(1,1)   

    def test_exit(self):   
        self.assertEqual(1,1)   

    def test_context(self):   
        self.assertEqual(1,1)   

然而,即使使用这些简单的测试,我也会收到此错误:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

经过研究,我发现这是一种分段错误。

作为参考,现在我的实际程序(运行没有问题)启动:

if __name__ == '__main__':
        appInstance = QtWidgets.QApplication(sys.argv)
        dialogInstance = QtWidgets.QMainWindow()
        programInstance = mainWindow(dialogInstance)
        dialogInstance.show()
        sys.exit(appInstance.exec_())   

我搞砸了如何创建程序实例?我非常难过,在Windows和Linux上遇到同样的错误。

我试图将崩溃的可能范围减少到3个小文件:

UI.py:

 from PyQt5 import QtCore, QtGui, QtWidgets

 class UI(object):
 def setupUi(self, MainWindow):
     MainWindow.setObjectName("MainWindow")
     MainWindow.resize(909, 710)
     self.centralwidget = QtWidgets.QWidget(MainWindow)
     self.centralwidget.setObjectName("centralwidget")
     self.artistFilter = QtWidgets.QLineEdit()
     self.artistFilter.setGeometry(QtCore.QRect(10, 20, 171, 31))
     self.artistFilter.setObjectName("artistFilter")

     MainWindow.setCentralWidget(self.centralwidget)
     self.statusbar = QtWidgets.QStatusBar(MainWindow)
     self.statusbar.setObjectName("statusbar")
     MainWindow.setStatusBar(self.statusbar)
     self.retranslateUi(MainWindow)
     QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
     _translate = QtCore.QCoreApplication.translate
     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

application.py:

import sys     
from PyQt5 import QtCore, QtGui, QtWidgets     
from UI import UI     
import datetime, sys, traceback     
from PyQt5.QtGui import *     
from PyQt5.QtWidgets import *     
from PyQt5.QtCore import *     

class mainWindow(UI):     
  def __init__(self, dialog):     
    UI.__init__(self)     
    self.setupUi(dialog)     

  def testFunction(self):     
    return 1     

if __name__ == '__main__':     
  app = QtWidgets.QApplication(sys.argv)     
  dialog = QtWidgets.QMainWindow()     
  prog = mainWindow(dialog)     
  dialog.show()     

  sys.exit(app.exec_())

test_mainWindow.py:

from unittest import TestCase
from PyQt5 import QtCore, QtGui, QtWidgets
import application
import sys
from PyQt5.QtTest import QTest
class TestMainWindow(TestCase):
    def setUp(self):
        app = QtWidgets.QApplication(sys.argv)
        dialog = QtWidgets.QMainWindow()
        self.prog = application.mainWindow(dialog)
        dialog.show()

    def test_SecondExample(self):
        QTest.keyClicks(self.prog.artistFilter, "Grasdateful Dead")
        self.assertNotEqual(self.prog.artistFilter.text(), 'Graasdteful Dead')

    def test_Example(self):
        QTest.keyClicks(self.prog.artistFilter, "Grateful Dead")
        self.assertEqual(self.prog.artistFilter.text(), 'Grateful Dead')

0 个答案:

没有答案