Skip to content

Commit

Permalink
[nrf fromtree] tests: drivers: can: host: allow specifying context al…
Browse files Browse the repository at this point in the history
…ong 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>
(cherry picked from commit afb2791)
  • Loading branch information
henrikbrixandersen authored and anangl committed Jun 13, 2024
1 parent c53780b commit 27c0cfd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
19 changes: 13 additions & 6 deletions tests/drivers/can/host/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
********************
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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:

Expand Down
9 changes: 8 additions & 1 deletion tests/drivers/can/host/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 27c0cfd

Please sign in to comment.