title: python running glib mainloop in unittest
body: <p>I'm new to python and trying to set up a functional test environment. The test environment shall receive signals over DBus and evaluate them. The DBus signaling uses the GLib MainLoop. I have the following class encapsulating the loop:</p>
  
  <pre><code>class SignalLoop(threading.Thread):
      def __init__(self):
          threading.Thread.__init__(self)
          self.__l = gobject.MainLoop()        
      def run(self):
          self.__l.run()
      def quit(self):
          self.__l.quit() 
  </code></pre>
  
  <p>and in the module doing the DBus handling I tried:</p>
  
  <pre><code>class __ModuleInitializer:
      def __init__(self):
          print('Module was initialized')
          gobject.threads_init()
          sl = SignalLoop()
          sl.start()
  
      def __del__(self):
          print('Module was deinitialized')
          sl.quit()
  
  __module_init = __ModuleInitializer()
  </code></pre>
  
  <p>I also tried the following:</p>
  
  <ul>
  <li>setUp/tearDownModule() does not work for me - at least in python 2.5.2</li>
  <li>__init__() and __del__() methods and putting all the testcases in a single class. The __del__() never get called and this solution will not scale with a lot of test cases.</li>
  </ul>
  
  <p>When I run the code, the sl.qui() never gets executed and I don't know why. I have to kill my main when running from console because it never returns. However this is not a problem for the PyDev test runner.</p>
  
  <p>Is there any hook which I can use for destructing the test environment? I want to be able to run a single test and a couple of tests as well, so "hacking" it into the methods itself is not an option.</p>
  
  <p>Can you please help me?</p>