<div dir="ltr">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.<div>
<br></div><div>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.</div>
<div><br></div><div>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.</div><div><br></div><div>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?</div>
<div><br></div><div>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.</div><div><br></div><div>Thanks,</div><div>
<br></div><div>-Dan<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Oct 29, 2013 at 6:21 PM, Aaron Richiger <span dir="ltr"><<a href="mailto:a.richi@bluewin.ch" target="_blank">a.richi@bluewin.ch</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000"><div><div class="h5">
    <div>Hello Dan!<br>
      <br>
      Sorry for not having replied to your question so far... Don't
      overestimate our testing capabilities;-)<br>
      Below, you can see some example code with a class with multiple
      tests. Hope it helps...<br>
      Aaron<br>
      <br>
      import unittest<br>
      import sys<br>
      <br>
      from PySide.QtGui import *<br>
      from PySide.QtCore import *<br>
      from PySide.QtTest import *<br>
      <br>
      <br>
      class TestDemo(unittest.TestCase):<br>
      <br>
          @classmethod<br>
          def setUpClass(cls):<br>
              try:<br>
                  cls.app = QApplication(sys.argv)<br>
              except:<br>
                  pass<br>
      <br>
          def test_widget_1(self):<br>
              w1 = QPushButton()<br>
              w1.clicked.connect(self.slot)<br>
              QTest.mouseClick(w1, Qt.LeftButton)<br>
              self.assertTrue(self.clicked)<br>
      <br>
          def test_widget_2(self):<br>
              w2 = QPushButton()<br>
              w2.clicked.connect(self.slot)<br>
              QTest.mouseClick(w2, Qt.LeftButton)<br>
              self.assertTrue(self.clicked)<br>
      <br>
          def slot(self):<br>
              self.clicked = True<br>
      <br>
      <br>
      if __name__ == '__main__':<br>
          unittest.main()<br>
      <br>
      <br>
      <br>
      <br>
      Am 29.10.2013 20:33, schrieb Dan McCombs:<br>
    </div>
    </div></div><blockquote type="cite">
      <div dir="ltr">Ok, let me ask this question -
        <div><div class="h5"><div><br>
        </div>
        <div>Someone out there must be unit testing their PySide code.
          Could you give an example of a test class with multiple
          separate tests?</div>
        <div><br>
        </div>
        <div>Thanks!</div>
        <div><br>
        </div>
        <div>-Dan</div>
      </div></div></div><div><div class="h5">
      <div class="gmail_extra"><br clear="all">
        <div>
          <div dir="ltr">
            <div><font size="1">--</font></div>
            <div>Dan McCombs</div>
            <div>Senior Software Engineer / Dyn</div>
            <div><a href="http://dyn.com/" target="_blank">http://dyn.com/</a></div>
            <div><br>
            </div>
            <div>Are you prepared for website disaster? Find out in two
              minutes:<br>
              <a href="http://dyn.com/dynedu-disaster-planning/" style="color:rgb(17,85,204)" target="_blank">http://dyn.com/dynedu-disaster-planning/</a><br>
            </div>
          </div>
        </div>
        <br>
        <br>
        <div class="gmail_quote">On Sat, Oct 26, 2013 at 8:01 AM, Dan
          McCombs <span dir="ltr"><<a href="mailto:dmccombs@dyn.com" target="_blank">dmccombs@dyn.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div dir="ltr">Hi Matthew,
              <div><br>
              </div>
              <div>Yes, the first thing I tried was:</div>
              <div>
                <div><br>
                </div>
                <div>   app = QtGui.QApplication([])</div>
                <div>   ...</div>
                <div>   del app</div>
              </div>
              <div><br>
              </div>
              <div>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.</div>
              <div><br>
              </div>
              <div>-Dan</div>
              <div><br>
              </div>
            </div>
            <div class="gmail_extra"><br clear="all">
              <div>
                <div dir="ltr">
                  <div><font size="1">--</font></div>
                  <div>Dan McCombs</div>
                  <div>Senior Software Engineer / Dyn</div>
                  <div><a href="http://dyn.com/" target="_blank">http://dyn.com/</a></div>
                  <div><br>
                  </div>
                  <div>Are you prepared for website disaster? Find out
                    in two minutes:<br>
                    <a href="http://dyn.com/dynedu-disaster-planning/" style="color:rgb(17,85,204)" target="_blank">http://dyn.com/dynedu-disaster-planning/</a><br>
                  </div>
                </div>
              </div>
              <div>
                <div> <br>
                  <br>
                  <div class="gmail_quote">On Fri, Oct 25, 2013 at 3:13
                    PM, Dan McCombs <span dir="ltr"><<a href="mailto:dmccombs@dyn.com" target="_blank">dmccombs@dyn.com</a>></span>
                    wrote:<br>
                    <blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      <div>
                        <div>
                          <div dir="ltr">Hey all,<br>
                            <div class="gmail_quote">
                              <div dir="ltr">
                                <div><br>
                                </div>
                                <div>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:</div>
                                <div><br>
                                </div>
                                <div><a href="http://stuvel.eu/blog/127/multiple-instances-of-qapplication-in-one-process" target="_blank">http://stuvel.eu/blog/127/multiple-instances-of-qapplication-in-one-process</a></div>

                                <div><br>
                                </div>
                                <div>I've tried doing something similar
                                  in PySide with the following lines in
                                  my setUp for each test:</div>
                                <div><br>
                                </div>
                                <div>QtGui.qApp = QtGui.QApplication([])</div>
                                <div><br>
                                </div>
                                <div>And the following in my tearDown:</div>
                                <div><br>
                                </div>
                                <div>QtGui.quit()</div>
                                <div>QtGui.qApp = none</div>
                                <div><br>
                                </div>
                                <div>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.".</div>
                                <div><br>
                                </div>
                                <div>How can I fully remove the
                                  QApplication instance between tests?</div>
                                <div><br>
                                </div>
                                <div>Thanks,</div>
                                <div><br>
                                </div>
                                <div>-Dan</div>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </blockquote>
                  </div>
                  <br>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
      <br>
      <fieldset></fieldset>
      <br>
      </div></div><div class="im"><pre>_______________________________________________
PySide mailing list
<a href="mailto:PySide@qt-project.org" target="_blank">PySide@qt-project.org</a>
<a href="http://lists.qt-project.org/mailman/listinfo/pyside" target="_blank">http://lists.qt-project.org/mailman/listinfo/pyside</a>
</pre>
    </div></blockquote>
    <br>
  </div>

<br>_______________________________________________<br>
PySide mailing list<br>
<a href="mailto:PySide@qt-project.org">PySide@qt-project.org</a><br>
<a href="http://lists.qt-project.org/mailman/listinfo/pyside" target="_blank">http://lists.qt-project.org/mailman/listinfo/pyside</a><br>
<br></blockquote></div><br></div></div>