-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new dbusmock.pytest_fixtures module which exports an initial set of fixtures. These mostly just wrap DbusTestCase into a fixture, plus some convenience functions for starting a system or session bus. In the future we may have more fixtures to spawn templates, etc. Ensure that all of our unit tests run with pytest. As that is expensive, only run it for TEST_CODE=1 in Fedora stable. On the others, only run the basic TestAPI checks and the fixtures checks. Thanks to @whot for the initial investigation and proposal!
- Loading branch information
1 parent
a1e345a
commit c25ce84
Showing
5 changed files
with
82 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'''pytest fixtures for DBusMock''' | ||
|
||
# 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. | ||
|
||
__author__ = 'Martin Pitt' | ||
__copyright__ = '(c) 2023 Martin Pitt <[email protected]>' | ||
|
||
from typing import Iterator | ||
|
||
import pytest | ||
|
||
import dbusmock.testcase | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def dbusmock_testcase() -> Iterator[dbusmock.testcase.DBusTestCase]: | ||
'''Export the whole DBusTestCase as a fixture.''' | ||
|
||
testcase = dbusmock.testcase.DBusTestCase() | ||
yield testcase | ||
testcase.tearDownClass() | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def dbusmock_testcase_system(dbusmock_testcase) -> Iterator[dbusmock.testcase.DBusTestCase]: | ||
'''Export the whole DBusTestCase as a fixture, with system bus started''' | ||
|
||
dbusmock_testcase.start_system_bus() | ||
yield dbusmock_testcase | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def dbusmock_testcase_session(dbusmock_testcase) -> Iterator[dbusmock.testcase.DBusTestCase]: | ||
'''Export the whole DBusTestCase as a fixture, with session bus started''' | ||
|
||
dbusmock_testcase.start_session_bus() | ||
yield dbusmock_testcase |
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_fixtures" |
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
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,29 @@ | ||
import subprocess | ||
|
||
import dbusmock | ||
|
||
|
||
def test_dbusmock_testcase_spawn_server(dbusmock_testcase_session): | ||
test_iface = 'org.freedesktop.Test.Main' | ||
|
||
p_mock = dbusmock_testcase_session.spawn_server( | ||
'org.freedesktop.Test', '/', test_iface, stdout=subprocess.DEVNULL) | ||
|
||
obj_test = dbusmock_testcase_session.get_dbus().get_object('org.freedesktop.Test', '/') | ||
|
||
obj_test.AddMethod('', 'Upper', 's', 's', 'ret = args[0].upper()', interface_name=dbusmock.MOCK_IFACE) | ||
assert obj_test.Upper('hello', interface=test_iface) == 'HELLO' | ||
|
||
p_mock.terminate() | ||
p_mock.wait() | ||
|
||
|
||
def test_dbusmock_testcase_spawn_system_template(dbusmock_testcase_system): | ||
p_mock, _obj = dbusmock_testcase_system.spawn_server_template('upower', stdout=subprocess.DEVNULL) | ||
|
||
out = subprocess.check_output(['upower', '--dump'], universal_newlines=True) | ||
assert 'version:' in out | ||
assert '0.99' in out | ||
|
||
p_mock.terminate() | ||
p_mock.wait() |