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 <hebad@vestas.com>
This commit is contained in:
Henrik Brix Andersen 2024-06-01 21:24:59 +00:00 committed by David Leach
commit afb2791ccf
2 changed files with 21 additions and 7 deletions

View file

@ -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:

View file

@ -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