From c3ec11e5f7636ebe0912b43f59804aae326ee917 Mon Sep 17 00:00:00 2001 From: Daniele Forsi IU5HKX Date: Sun, 29 Jun 2025 19:18:48 +0200 Subject: [PATCH] Allow to execute some tests with a simulator or with a real rig To execute the tests with the installed Hamlib use the same long arguments as rigctl, eg: bindings/python/test_rig.py --model {MODEL_NUMBER} --rig-file /dev/ttyUSB0 --serial-speed {BAUD} To execute the tests from the build tree, add the path to the libraries that you built, eg. from the root of the build tree: PYTHONPATH=bindings/:bindings/.libs/ ...your command... --- bindings/python/conftest.py | 30 ++++++++++++++++++++++++++++++ bindings/python/test_rig.py | 29 +++++++++++++++++------------ 2 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 bindings/python/conftest.py diff --git a/bindings/python/conftest.py b/bindings/python/conftest.py new file mode 100644 index 000000000..d3d435e98 --- /dev/null +++ b/bindings/python/conftest.py @@ -0,0 +1,30 @@ +"""Tests of the Python bindings for Hamlib +""" +import pytest + +def pytest_addoption(parser): + # using long options only because short options conflict with pytest's + parser.addoption('--model', type=int, default=1, + metavar='ID', help='select radio model number') + parser.addoption('--rig-file', default=None, + metavar='DEVICE', help='set device of the radio to operate on') + parser.addoption('--serial-speed', type=int, default=0, + metavar='BAUD', help='set serial speed of the serial port') + parser.addoption('--hamlib-verbose', action='count', default=0, + help='set verbose mode, cumulative') + +@pytest.fixture +def model(request): + return request.config.getoption("--model") + +@pytest.fixture +def rig_file(request): + return request.config.getoption("--rig-file") + +@pytest.fixture +def serial_speed(request): + return request.config.getoption("--serial-speed") + +@pytest.fixture +def hamlib_verbose(request): + return request.config.getoption("--hamlib-verbose") diff --git a/bindings/python/test_rig.py b/bindings/python/test_rig.py index 1c3927cb0..2de41ec71 100755 --- a/bindings/python/test_rig.py +++ b/bindings/python/test_rig.py @@ -5,20 +5,18 @@ Running this script directly will use the installed bindings. For an in-tree run use "make check", or set PYTHONPATH to point to the directories containing Hamlib.py and _Hamlib.so. """ -from pytest import raises +import pytest import Hamlib Hamlib.rig_set_debug(Hamlib.RIG_DEBUG_NONE) -RIG_MODEL = Hamlib.RIG_MODEL_DUMMY - class TestClass: """Container class for tests""" - def test_without_open(self): + def test_without_open(self, model): """Call all the methods that do not depend on open()""" - rig = Hamlib.Rig(RIG_MODEL) + rig = Hamlib.Rig(model) assert rig is not None assert rig.do_exception == 0 assert rig.error_status == Hamlib.RIG_OK @@ -35,17 +33,22 @@ class TestClass: assert isinstance(conf, str) assert rig.set_conf("mcfg", "foo") is None conf = rig.get_conf("mcfg") - assert conf == "foo" + if model == Hamlib.RIG_MODEL_DUMMY: + assert conf == "foo" + else: + assert conf == "" assert rig.token_lookup("") is None - def test_with_open(self): + def test_with_open(self, model, rig_file, serial_speed): """Call all the methods that depend on open()""" - rig = Hamlib.Rig(RIG_MODEL) + rig = Hamlib.Rig(model) assert rig is not None assert rig.state.comm_state == 0 + assert rig.set_conf("rig_pathname", rig_file) is None + assert rig.set_conf("serial_speed", str(serial_speed)) is None assert rig.open() is None assert rig.state.comm_state == 1 info = rig.get_info() @@ -70,9 +73,10 @@ class TestClass: assert info is None - def test_misc(self): + @pytest.mark.skipif('config.getoption("model") != Hamlib.RIG_MODEL_DUMMY') + def test_misc(self, model): """Just call all the methods""" - rig = Hamlib.Rig(RIG_MODEL) + rig = Hamlib.Rig(model) assert rig is not None assert rig.close() is None @@ -213,9 +217,10 @@ class TestClass: assert rig.vfo_op(0, 0) is None - def test_object_creation(self): + @pytest.mark.skipif('config.getoption("model") != Hamlib.RIG_MODEL_DUMMY') + def test_object_creation(self, model): """Create all objects available""" - rig = Hamlib.Rig(RIG_MODEL) + rig = Hamlib.Rig(model) assert rig is not None assert isinstance(rig.caps, Hamlib.rig_caps)