@@ -16,47 +16,76 @@ Python-sdbus provides several utilities to enable unit testing.
1616
1717 Requires ``dbus-daemon `` executable be installed.
1818
19+ Example::
20+
21+ from sdbus import DbusInterfaceCommonAsync, dbus_method_async
22+ from sdbus.unittest import IsolatedDbusTestCase
23+
24+ class TestInterface(DbusInterfaceCommonAsync,
25+ interface_name='org.test.test',
26+ ):
27+
28+ @dbus_method_async("s", "s")
29+ async def upper(self, string: str) -> str:
30+ """Uppercase the input"""
31+ return string.upper()
32+
33+ def initialize_object() -> Tuple[TestInterface, TestInterface]:
34+ test_object = TestInterface()
35+ test_object.export_to_dbus('/')
36+
37+ test_object_connection = TestInterface.new_proxy(
38+ "org.example.test", '/')
39+
40+ return test_object, test_object_connection
41+
42+
43+ class TestProxy(IsolatedDbusTestCase):
44+ async def asyncSetUp(self) -> None:
45+ await super().asyncSetUp()
46+ await self.bus.request_name_async("org.example.test", 0)
47+
48+ async def test_method_kwargs(self) -> None:
49+ test_object, test_object_connection = initialize_object()
50+
51+ self.assertEqual(
52+ 'TEST',
53+ await test_object_connection.upper('test'),
54+ )
55+
1956 .. py :attribute :: bus
2057 :type: SdBus
2158
2259 Bus instance connected to isolated D-Bus environment.
2360
2461 It is also set as a default bus.
2562
63+ .. py :method :: assertDbusSignalEmits(signal, timeout = 1 )
64+
65+ Assert that a given signal was emitted at least once within the
66+ given timeout.
67+
68+ :param signal: D-Bus signal object. Can be a signal from either local or proxy object.
69+ :param Union[int, float] timeout: Maximum wait time until first captured signal.
2670
27- Usage example: ::
71+ Should be used as an async context manager. The context manager exits as soon
72+ as first signal is captured.
2873
29- from sdbus import DbusInterfaceCommonAsync, dbus_method_async
30- from sdbus.unittest import IsolatedDbusTestCase
74+ The object returned by context manager has following attributes:
3175
32- class TestInterface(DbusInterfaceCommonAsync,
33- interface_name='org.test.test',
34- ):
76+ .. py :attribute :: output
77+ :type: List[Any]
3578
36- @dbus_method_async("s", "s")
37- async def upper(self, string: str) -> str:
38- """Uppercase the input"""
39- return string.upper()
79+ List of captured data.
4080
41- def initialize_object() -> Tuple[TestInterface, TestInterface]:
42- test_object = TestInterface()
43- test_object.export_to_dbus('/')
81+ Example::
4482
45- test_object_connection = TestInterface.new_proxy(
46- "org.example. test", '/' )
83+ async with self.assertDbusSignalEmits(test_object.test_signal) as signal_record:
84+ test_object.test_signal.emit(" test")
4785
48- return test_object, test_object_connection
86+ self.assertEqual(["test"], signal_record.output)
4987
88+ *New in version 0.12.0. *
5089
51- class TestProxy(IsolatedDbusTestCase):
52- async def asyncSetUp(self) -> None:
53- await super().asyncSetUp()
54- await self.bus.request_name_async("org.example.test", 0)
5590
56- async def test_method_kwargs(self) -> None:
57- test_object, test_object_connection = initialize_object()
5891
59- self.assertEqual(
60- 'TEST',
61- await test_object_connection.upper('test'),
62- )
0 commit comments