您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何为用Python编写的DBUS服务编写功能测试?

如何为用Python编写的DBUS服务编写功能测试?

在Ali A的帖子的帮助下,我设法解决了我的问题。需要将阻塞事件循环启动到一个单独的进程中,以便它可以侦听事件而不会阻塞测试。

请注意,我的问题标题包含一些错误的术语,我正在尝试编写功能测试,而不是单元测试。我意识到了这种区别,但直到后来才意识到自己的错误

我已经调整了问题中的示例。它大致类似于“ test_pidavim.py”示例,但是对“ dbus.glib”使用导入来处理glib循环依赖关系,而不是在所有DBusGMainLoop内容中进行编码:

import unittest

import os
import sys
import subprocess
import time

import dbus
import dbus.service
import dbus.glib
import gobject

class 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()
python 2022/1/1 18:39:30 有250人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶