File: unittest.rst

package info (click to toggle)
python-sdbus 0.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 980 kB
  • sloc: python: 7,783; ansic: 2,524; makefile: 9; sh: 4
file content (91 lines) | stat: -rw-r--r-- 2,797 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Unit testing
============

Python-sdbus provides several utilities to enable unit testing.

.. py:currentmodule:: sdbus.unittest

.. py:class:: IsolatedDbusTestCase

    Extension of `unittest.IsolatedAsyncioTestCase
    <https://docs.python.org/3/library/unittest.html#unittest.IsolatedAsyncioTestCase>`__
    from standard library.

    Creates an isolated instance of session D-Bus. The D-Bus will be closed
    and cleaned up after tests are finished.

    Requires ``dbus-daemon`` executable be installed.

    Example::

        from sdbus import DbusInterfaceCommonAsync, dbus_method_async
        from sdbus.unittest import IsolatedDbusTestCase

        class TestInterface(DbusInterfaceCommonAsync,
                            interface_name='org.test.test',
                            ):

            @dbus_method_async("s", "s")
            async def upper(self, string: str) -> str:
                """Uppercase the input"""
                return string.upper()

        def initialize_object() -> tuple[TestInterface, TestInterface]:
            test_object = TestInterface()
            test_object.export_to_dbus('/')

            test_object_connection = TestInterface.new_proxy(
            "org.example.test", '/')

            return test_object, test_object_connection


        class TestProxy(IsolatedDbusTestCase):
            async def asyncSetUp(self) -> None:
                await super().asyncSetUp()
                await self.bus.request_name_async("org.example.test", 0)

            async def test_method_kwargs(self) -> None:
                test_object, test_object_connection = initialize_object()

                self.assertEqual(
                    'TEST',
                    await test_object_connection.upper('test'),
                )

    .. py:attribute:: bus
        :type: SdBus

        Bus instance connected to isolated D-Bus environment.

        It is also set as a default bus.

    .. py:method:: assertDbusSignalEmits(signal, timeout=1)

      Assert that a given signal was emitted at least once within the
      given timeout.

      :param signal: D-Bus signal object. Can be a signal from either local or proxy object.
      :param Union[int, float] timeout: Maximum wait time until first captured signal.

      Should be used as an async context manager. The context manager exits as soon
      as first signal is captured.

      The object returned by context manager has following attributes:

      .. py:attribute:: output
        :type: list[Any]

        List of captured data.

      Example::

        async with self.assertDbusSignalEmits(test_object.test_signal) as signal_record:
            test_object.test_signal.emit("test")

        self.assertEqual(["test"], signal_record.output)

      *New in version 0.12.0.*