From afb2791ccf9ab8e2366dbd881d9abf82dc578c82 Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Sat, 1 Jun 2024 21:24:59 +0000 Subject: [PATCH] tests: drivers: can: host: allow specifying context along with fixture Allow specifying the python-can configuration context to use along with the "can" fixture. This opens up for specifying board-specific contexts in the twister hardware map file. Signed-off-by: Henrik Brix Andersen --- tests/drivers/can/host/README.rst | 19 +++++++++++++------ tests/drivers/can/host/pytest/conftest.py | 9 ++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/drivers/can/host/README.rst b/tests/drivers/can/host/README.rst index b6a3fcef5c8..7f8b15936af 100644 --- a/tests/drivers/can/host/README.rst +++ b/tests/drivers/can/host/README.rst @@ -28,10 +28,17 @@ The Zephyr end of the CAN fixture can be configured as follows: The host end of the CAN fixture can be configured through python-can. Available configuration options depend on the type of host CAN adapter used. The python-can library provides a lot of -flexibility for configuration as decribed in the `python-can configuration`_ page. By default, the -python-can configuration context is not specified, causing python-can to use the default -configuration context. The context can be overridden using the ``--can-context`` test suite argument -(see examples below). +flexibility for configuration as decribed in the `python-can configuration`_ page, all centered +around the concept of a configuration "context. The configuration context for this test suite can be +configured as follows: + +* By default, the python-can configuration context is not specified, causing python-can to use the + default configuration context. +* A specific configuration context can be provided along with the ``can`` fixture separated by a + ``:`` (i.e. specify fixture ``can:zcan0`` to use the ``zcan0`` python-can configuration context). +* The configuration context can be overridden using the ``--can-context`` test suite argument + (i.e. run ``twister`` with the ``--pytest-args=--can-context=zcan0`` argument to use the ``zcan0`` + python-can configuration context). Building and Running ******************** @@ -65,7 +72,7 @@ be launched using Twister: .. code-block:: shell - west twister -v -p native_sim/native/64 -X can -T tests/drivers/can/host/ --pytest-args=--can-context=zcan0 + west twister -v -p native_sim/native/64 -X can:zcan0 -T tests/drivers/can/host/ After the test suite has completed, the virtual SocketCAN interface can be removed again: @@ -107,7 +114,7 @@ Twister. Below is an example for running on the :ref:`lpcxpresso55s36`: .. code-block:: shell - west twister -v -p lpcxpresso55s36/lpc55s36 --device-testing --device-serial /dev/ttyACM0 -X can -T tests/drivers/can/host/ --pytest-args=--can-context=can0 + west twister -v -p lpcxpresso55s36/lpc55s36 --device-testing --device-serial /dev/ttyACM0 -X can:can0 -T tests/drivers/can/host/ After the test suite has completed, the SocketCAN interface can be brought down again: diff --git a/tests/drivers/can/host/pytest/conftest.py b/tests/drivers/can/host/pytest/conftest.py index f7ffe763a69..691e39fc497 100644 --- a/tests/drivers/can/host/pytest/conftest.py +++ b/tests/drivers/can/host/pytest/conftest.py @@ -23,9 +23,16 @@ def pytest_addoption(parser) -> None: help='Configuration context to use for python-can (default: None)') @pytest.fixture(name='context', scope='session') -def fixture_context(request) -> str: +def fixture_context(request, dut: DeviceAdapter) -> str: """Return the name of the python-can configuration context to use.""" ctx = request.config.getoption('--can-context') + + if ctx is None: + for fixture in dut.device_config.fixtures: + if fixture.startswith('can:'): + ctx = fixture.split(sep=':', maxsplit=1)[1] + break + logger.info('using python-can configuration context "%s"', ctx) return ctx