在Ali A的帖子的帮助下,我设法解决了我的问题。需要将阻塞事件循环启动到一个单独的进程中,以便它可以侦听事件而不会阻塞测试。
请注意,我的问题标题包含一些错误的术语,我正在尝试编写功能测试,而不是单元测试。我意识到了这种区别,但直到后来才意识到自己的错误。
我已经调整了问题中的示例。它大致类似于“ test_pidavim.py”示例,但是对“
dbus.glib”使用导入来处理glib循环依赖关系,而不是在所有DBusGMainLoop内容中进行编码:
import unittestimport osimport sysimport subprocessimport timeimport dbusimport dbus.serviceimport dbus.glibimport gobjectclass MyDBUSService(dbus.service.Object): def __init__(self): bus_name = dbus.service.BusName('test.helloservice', bus = dbus.SessionBus()) dbus.service.Object.__init__(self, bus_name, '/test/helloservice') def listen(self): loop = gobject.MainLoop() loop.run() @dbus.service.method('test.helloservice') def hello(self): return "Hello World!"class baseTestCase(unittest.TestCase): def setUp(self): env = os.environ.copy() self.p = subprocess.Popen(['python', './dbus_practice.py', 'server'], env=env) # Wait for the service to become available time.sleep(1) assert self.p.stdout == None assert self.p.stderr == None def testHelloService(self): bus = dbus.SessionBus() helloservice = bus.get_object('test.helloservice', '/test/helloservice') hello = helloservice.get_dbus_method('hello', 'test.helloservice') assert hello() == "Hello World!" def tearDown(self): # terminate() not supported in Python 2.5 #self.p.terminate() os.kill(self.p.pid, 15)if __name__ == '__main__': arg = "" if len(sys.argv) > 1: arg = sys.argv[1] if arg == "server": myservice = MyDBUSService() myservice.listen() else: unittest.main()


