栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

在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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/662725.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号