-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a pytest module to provide a simple fixture
- Loading branch information
Showing
4 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'''pytest convenience methods for DBusMocks''' | ||
|
||
# This program is free software; you can redistribute it and/or modify it under | ||
# the terms of the GNU Lesser General Public License as published by the Free | ||
# Software Foundation; either version 3 of the License, or (at your option) any | ||
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text | ||
# of the license. | ||
|
||
import pytest | ||
from typing import Iterator, Optional | ||
|
||
import pytest | ||
import dbus | ||
import dbusmock | ||
|
||
|
||
class BusMock(dbusmock.DBusTestCase): | ||
def __init__(self): | ||
super().__init__() | ||
self._dbus_con: Optional[dbus.Bus] = None | ||
|
||
@property | ||
def dbus_con(self) -> "dbus.Bus": | ||
''' | ||
Returns the dbus.bus.BusConnection() for this object. This returns either | ||
the session bus connection or the system bus connection, depending on the | ||
object. | ||
''' | ||
assert self._dbus_con is not None | ||
return self._dbus_con | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def session_mock() -> Iterator[BusMock]: | ||
''' | ||
Fixture to yield a DBusTestCase with a started session bus. | ||
''' | ||
bus = BusMock() | ||
bus.setUp() | ||
bus.start_session_bus() | ||
bus._dbus_con = bus.get_dbus(system_bus=False) | ||
yield bus | ||
bus.tearDown() | ||
bus.tearDownClass() | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def system_mock() -> Iterator[BusMock]: | ||
''' | ||
Fixture to yield a DBusTestCase with a started session bus. | ||
''' | ||
bus = BusMock() | ||
bus.setUp() | ||
bus.start_system_bus() | ||
bus._dbus_con = bus.get_dbus(system_bus=True) | ||
yield bus | ||
bus.tearDown() | ||
bus.tearDownClass() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pytest_plugins = "dbusmock.pytest" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/python3 | ||
|
||
# This program is free software; you can redistribute it and/or modify it under | ||
# the terms of the GNU Lesser General Public License as published by the Free | ||
# Software Foundation; either version 3 of the License, or (at your option) any | ||
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text | ||
# of the license. | ||
|
||
from pathlib import Path | ||
import dbus | ||
import dbusmock | ||
import re | ||
import tempfile | ||
|
||
try: | ||
from dbusmock.pytest import session_mock | ||
import pytest | ||
|
||
@pytest.fixture | ||
def mock(session_mock): | ||
# pylint: disable=consider-using-with | ||
session_mock.mock_log = tempfile.NamedTemporaryFile() | ||
session_mock.p_mock = session_mock.spawn_server('org.freedesktop.Test', | ||
'/', | ||
'org.freedesktop.Test.Main', | ||
stdout=session_mock.mock_log) | ||
|
||
session_mock.obj_test = session_mock.dbus_con.get_object('org.freedesktop.Test', '/') | ||
session_mock.dbus_test = dbus.Interface(session_mock.obj_test, 'org.freedesktop.Test.Main') | ||
session_mock.dbus_mock = dbus.Interface(session_mock.obj_test, dbusmock.MOCK_IFACE) | ||
session_mock.dbus_props = dbus.Interface(session_mock.obj_test, dbus.PROPERTIES_IFACE) | ||
yield session_mock | ||
|
||
if session_mock.p_mock.stdout: | ||
session_mock.p_mock.stdout.close() | ||
session_mock.p_mock.terminate() | ||
session_mock.p_mock.wait() | ||
|
||
|
||
class TestPytestAPI: | ||
def test_noarg_noret(self, mock): | ||
'''no arguments, no return value''' | ||
|
||
mock.dbus_mock.AddMethod('', 'Do', '', '', '') | ||
assert mock.dbus_test.Do() == None | ||
|
||
# check that it's logged correctly | ||
log = Path(mock.mock_log.name,).read_bytes() | ||
assert re.match(rb'^[0-9.]+ Do$', log) | ||
|
||
except ImportError: | ||
pass |