diff --git a/dbusmock/pytest_fixtures.py b/dbusmock/pytest_fixtures.py new file mode 100644 index 00000000..6a44c6c3 --- /dev/null +++ b/dbusmock/pytest_fixtures.py @@ -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 ' + +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 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 00000000..ab7ecbaf --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1 @@ +pytest_plugins = "dbusmock.pytest_fixtures" diff --git a/tests/run-debian b/tests/run-debian index 04d9ba9d..6fc8ad5b 100644 --- a/tests/run-debian +++ b/tests/run-debian @@ -13,7 +13,7 @@ eatmydata apt-get -y --purge dist-upgrade # install build dependencies eatmydata apt-get install --no-install-recommends -y git \ python3-all python3-setuptools python3-setuptools-scm python3-build python3-venv \ - python3-dbus python3-gi gir1.2-glib-2.0 \ + python3-dbus python3-pytest python3-gi gir1.2-glib-2.0 \ dbus libnotify-bin upower network-manager bluez ofono ofono-scripts # systemd's tools otherwise fail on "not been booted with systemd" @@ -27,6 +27,7 @@ export TEST_CODE="$TEST_CODE" cp -r $(pwd) /tmp/source cd /tmp/source python3 -m unittest -v +pytest -vv -k 'test_pytest or TestAPI' # massively parallel test to check for races for i in \$(seq 100); do ( PYTHONPATH=. python3 tests/test_api.py TestTemplates || touch /tmp/fail ) & diff --git a/tests/run-fedora b/tests/run-fedora index 6c0ec835..73386e67 100644 --- a/tests/run-fedora +++ b/tests/run-fedora @@ -2,7 +2,7 @@ set -eux # install build dependencies dnf -y install python3-setuptools python3 python3-gobject-base \ - python3-dbus dbus-x11 util-linux \ + python3-dbus python3-pytest dbus-x11 util-linux \ upower NetworkManager bluez libnotify polkit if ! grep -q :el /etc/os-release; then @@ -31,4 +31,12 @@ python3 -m unittest -v || { [ -z "$DEBUG" ] || sleep infinity exit 1 } + +# run all tests with pytest only on TEST_CODE, as that is expensive +if [ -n "$TEST_CODE"]; then + pytest -v +else + pytest -vv -k 'test_pytest or TestAPI' +fi +fi EOF diff --git a/tests/test_pytest.py b/tests/test_pytest.py new file mode 100644 index 00000000..9f7c46d3 --- /dev/null +++ b/tests/test_pytest.py @@ -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()