[PySide] Creating and deleting instances of QApplication within unit tests
Dan McCombs
dmccombs at dyn.com
Tue Nov 5 14:32:18 CET 2013
Thanks Aaron. When I have a single instance of QApplication in my tests,
and instantiate the window classes of my application more than once (for
instance, in each test case) my tests pass but Python segfaults on exit,
which is why I was looking at creating/destroying the QApplication instance
on each test run as I thought I was doing it wrong having the same one
through all my tests.
I tried to create a simple example reproducing this however, and it's not
segfaulting. It may be because I'm using a parent class with the borg
pattern in my application which contains a dictionary containing the
instances for each window, so that each window can easily access the other
window instances to show/hide them, etc.
I'll try doing that in my example to see if it segfaults. I'm guessing
something is being freed twice on exit in that case and causing the problem.
Assuming it is, what kind of patterns are people using for having multiple
windows that you want to reference from each other? Or is that just being
avoided?
An example would be there's a main window that needs to show another
window, that window needs call a method in the main window to load data in
a table, etc.
Thanks,
-Dan
On Tue, Oct 29, 2013 at 6:21 PM, Aaron Richiger <a.richi at bluewin.ch> wrote:
> Hello Dan!
>
> Sorry for not having replied to your question so far... Don't overestimate
> our testing capabilities;-)
> Below, you can see some example code with a class with multiple tests.
> Hope it helps...
> Aaron
>
> import unittest
> import sys
>
> from PySide.QtGui import *
> from PySide.QtCore import *
> from PySide.QtTest import *
>
>
> class TestDemo(unittest.TestCase):
>
> @classmethod
> def setUpClass(cls):
> try:
> cls.app = QApplication(sys.argv)
> except:
> pass
>
> def test_widget_1(self):
> w1 = QPushButton()
> w1.clicked.connect(self.slot)
> QTest.mouseClick(w1, Qt.LeftButton)
> self.assertTrue(self.clicked)
>
> def test_widget_2(self):
> w2 = QPushButton()
> w2.clicked.connect(self.slot)
> QTest.mouseClick(w2, Qt.LeftButton)
> self.assertTrue(self.clicked)
>
> def slot(self):
> self.clicked = True
>
>
> if __name__ == '__main__':
> unittest.main()
>
>
>
>
> Am 29.10.2013 20:33, schrieb Dan McCombs:
>
> Ok, let me ask this question -
>
> Someone out there must be unit testing their PySide code. Could you give
> an example of a test class with multiple separate tests?
>
> Thanks!
>
> -Dan
>
> --
> Dan McCombs
> Senior Software Engineer / Dyn
> http://dyn.com/
>
> Are you prepared for website disaster? Find out in two minutes:
> http://dyn.com/dynedu-disaster-planning/
>
>
> On Sat, Oct 26, 2013 at 8:01 AM, Dan McCombs <dmccombs at dyn.com> wrote:
>
>> Hi Matthew,
>>
>> Yes, the first thing I tried was:
>>
>> app = QtGui.QApplication([])
>> ...
>> del app
>>
>> But, the next test raises the exception about there already being a
>> QApplication instance when it runs that first line. That's why I resorted
>> to poking around trying to figure out where PySide is keeping track of that
>> original instance causing it to raise that exception.
>>
>> -Dan
>>
>>
>> --
>> Dan McCombs
>> Senior Software Engineer / Dyn
>> http://dyn.com/
>>
>> Are you prepared for website disaster? Find out in two minutes:
>> http://dyn.com/dynedu-disaster-planning/
>>
>>
>> On Fri, Oct 25, 2013 at 3:13 PM, Dan McCombs <dmccombs at dyn.com> wrote:
>>
>>> Hey all,
>>>
>>> I've been struggling with unit testing my PySide application. My tests
>>> run fine, but if I have more than one test, Python segfaults on quit. It
>>> seems the solution would be to destroy/create the QApplication instance on
>>> each test run, as I've seen people mentioning in the case of PyQT such as:
>>>
>>>
>>> http://stuvel.eu/blog/127/multiple-instances-of-qapplication-in-one-process
>>>
>>> I've tried doing something similar in PySide with the following lines
>>> in my setUp for each test:
>>>
>>> QtGui.qApp = QtGui.QApplication([])
>>>
>>> And the following in my tearDown:
>>>
>>> QtGui.quit()
>>> QtGui.qApp = none
>>>
>>> However, the instance still exists (I can get it via
>>> QtGui.QApplication.instance()) and when the second test setUp starts to
>>> run, I get an exception that "A QApplication instance already exists.".
>>>
>>> How can I fully remove the QApplication instance between tests?
>>>
>>> Thanks,
>>>
>>> -Dan
>>>
>>
>>
>
>
> _______________________________________________
> PySide mailing listPySide at qt-project.orghttp://lists.qt-project.org/mailman/listinfo/pyside
>
>
>
> _______________________________________________
> PySide mailing list
> PySide at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/pyside
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20131105/ac33763f/attachment.html>
More information about the PySide
mailing list