Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 134065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:32:45+00:00 2026-05-11T06:32:45+00:00

(Title was: How to write a unit test for a DBUS service written in

  • 0

(Title was: ‘How to write a unit test for a DBUS service written in Python?’)

I’ve started to write a DBUS service using dbus-python, but I’m having trouble writing a test case for it.

Here is an example of the test I am trying to create. Notice that I have put a GLib event loop in the setUp(), this is where the problem hits:

import unittest  import gobject import dbus import dbus.service import dbus.glib  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')      @dbus.service.method('test.helloservice')     def hello(self):         return 'Hello World!'   class BaseTestCase(unittest.TestCase):      def setUp(self):         myservice = MyDBUSService()         loop = gobject.MainLoop()         loop.run()         # === Test blocks here ===      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!'  if __name__ == '__main__':     unittest.main() 

My problem is that the DBUS implementation requires you to start an event loop so that it can start dispatching events. The common approach is to use GLib’s gobject.MainLoop().start() (although I’m not married to this approach, if someone has a better suggestion). If you don’t start an event loop, the service still blocks, and you also cannot query it.

If I start my service in the test, the event loop blocks the test from completing. I know the service is working because I can query the service externally using the qdbus tool, but I can’t automate this inside the test that starts it.

I’m considering doing some kind of process forking inside the test to handle this, but I was hoping someone might have a neater solution, or at least a good starting place for how I would write a test like this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:32:46+00:00Added an answer on May 11, 2026 at 6:32 am

    With some help from Ali A’s post, I have managed to solve my problem. The blocking event loop needed to be launched into a separate process, so that it can listen for events without blocking the test.

    Please be aware my question title contained some incorrect terminology, I was trying to write a functional test, as opposed to a unit test. I was aware of the distinction, but didn’t realise my mistake until later.

    I’ve adjusted the example in my question. It loosely resembles the ‘test_pidavim.py’ example, but uses an import for ‘dbus.glib’ to handle the glib loop dependencies instead of coding in all the DBusGMainLoop stuff:

    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() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 72k
  • Answers 72k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I have achieved it. Where selected value is in Hidden… May 11, 2026 at 1:37 pm
  • added an answer You should check out Larry Osterman's series on COM and… May 11, 2026 at 1:37 pm
  • added an answer This should do what you need: <Directory /APP> Order Allow,Deny… May 11, 2026 at 1:37 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.