LEW21 / pydbus

Pythonic DBus library
GNU Lesser General Public License v2.1
327 stars 76 forks source link

How to run tests ? #40

Closed M4rtinK closed 7 years ago

M4rtinK commented 7 years ago

I'm currently packaging pydbus for Fedora and the Fedora packaging guidelines recommend running tests during the package build if possible. So I've tried to run the pydbus tests with:

python3 setup.py test

But that results in a traceback:

$ python3 setup.py test
running test
running egg_info
creating pydbus.egg-info
writing pydbus.egg-info/PKG-INFO
writing dependency_links to pydbus.egg-info/dependency_links.txt
writing top-level names to pydbus.egg-info/top_level.txt
writing manifest file 'pydbus.egg-info/SOURCES.txt'
reading manifest file 'pydbus.egg-info/SOURCES.txt'
writing manifest file 'pydbus.egg-info/SOURCES.txt'
running build_ext
Traceback (most recent call last):
  File "setup.py", line 35, in <module>
    'Programming Language :: Python :: 2.7'
  File "/usr/lib64/python3.5/distutils/core.py", line 148, in setup
    dist.run_commands()
  File "/usr/lib64/python3.5/distutils/dist.py", line 955, in run_commands
    self.run_command(cmd)
  File "/usr/lib64/python3.5/distutils/dist.py", line 974, in run_command
    cmd_obj.run()
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 172, in run
    self.run_tests()
  File "/usr/lib64/python3.5/contextlib.py", line 77, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 151, in project_on_sys_path
    yield
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 172, in run
    self.run_tests()
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 193, in run_tests
    testRunner=self._resolve_as_ep(self.test_runner),
  File "/usr/lib64/python3.5/unittest/main.py", line 93, in __init__
    self.parseArgs(argv)
  File "/usr/lib64/python3.5/unittest/main.py", line 123, in parseArgs
    self._do_discovery([])
  File "/usr/lib64/python3.5/unittest/main.py", line 228, in _do_discovery
    self.test = loader.discover(self.start, self.pattern, self.top)
  File "/usr/lib64/python3.5/unittest/loader.py", line 341, in discover
    tests = list(self._find_tests(start_dir, pattern))
  File "/usr/lib64/python3.5/unittest/loader.py", line 398, in _find_tests
    full_path, pattern, namespace)
  File "/usr/lib64/python3.5/unittest/loader.py", line 475, in _find_test_path
    tests = self.loadTestsFromModule(package, pattern=pattern)
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 40, in loadTestsFromModule
    tests.append(self.loadTestsFromName(submodule))
  File "/usr/lib64/python3.5/unittest/loader.py", line 190, in loadTestsFromName
    return self.loadTestsFromModule(obj)
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 40, in loadTestsFromModule
    tests.append(self.loadTestsFromName(submodule))
  File "/usr/lib64/python3.5/unittest/loader.py", line 190, in loadTestsFromName
    return self.loadTestsFromModule(obj)
  File "/usr/lib/python3.5/site-packages/setuptools/command/test.py", line 40, in loadTestsFromModule
    tests.append(self.loadTestsFromName(submodule))
  File "/usr/lib64/python3.5/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "/home/mkolman/devel/misc/pydbus/pydbus/pydbus/examples/clientserver/client.py", line 12, in <module>
    the_object = bus.get("net.lew21.pydbus.ClientServerExample")
  File "/home/mkolman/devel/misc/pydbus/pydbus/pydbus/proxy.py", line 47, in get
    0, timeout_to_glib(timeout), None)
GLib.Error: g-dbus-error-quark: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name net.lew21.pydbus.ClientServerExample was not provided by any .service files (2)

Am I possibly running the pydbus tests incorrectly or is some additional setup required before the tests can be started (I see some docker files in the tests directory) ?

acaso commented 7 years ago

I guess unittest's autodiscover is loading examples/clientserver/client.py looking for tests and in that process the code gets executed (as the module lacks an if __name__=='main' condition). client.py is an example module that needs the example module server.py to also be running, that's why it fails in your case.

As they are now, pydbus tests are not meant to be discovered (although that may change in future releases).

pydbus' tests need to be run in a context where a session DBUS daemon is running. If you run the tests inside your desktop session, it will probably already be one, but that won't be the case if, for example, your package is tested in a build server, a chroot environment or a minimal docker container. tests/run.sh launches such daemon and runs the tests (also have a look at some improvements I proposed in https://github.com/LEW21/pydbus/pull/38). I guess your best bet would be using that script or some variation of it in the test phase of your package build.

I'm in the process of packaging pydbus for Debian, and have used such a script to run the tests.

LEW21 commented 7 years ago
./tests/run.sh

I've merged #38 and released 0.6.0, so it should work well now.

Thanks for packaging pydbus!