File size: 2,530 Bytes
cb65407 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | .. _howto-python-runner:
******************************
Building HDL and Running Tests
******************************
.. versionadded:: 1.8
.. warning::
Python runners and associated APIs are an experimental feature and subject to change.
The Python Test Runner (short: "runner") described here is a replacement
for cocotb's traditional Makefile flow.
It builds the HDL for the simulator and runs the tests.
It is not meant to be ideal solution for everyone.
You can easily integrate cocotb into your custom flow, see :ref:`custom-flows`.
Usage with pytest
=================
The runner can be used with `pytest <https://pytest.org>`_
which is Python's go-to testing tool.
For an example on how to set up the runner with pytest,
see the file
:file:`{cocotb-root}/examples/simple_dff/test_dff.py`,
with the relevant part shown here:
.. literalinclude:: ../../examples/simple_dff/test_dff.py
:language: python3
:start-at: def test_simple_dff_runner():
:end-at: runner.test(hdl_toplevel="dff", test_module="test_dff,")
You run this file with pytest like
.. code-block:: bash
SIM=questa HDL_TOPLEVEL_LANG=vhdl pytest examples/simple_dff/test_dff.py
Note that the environment variables ``SIM`` and ``HDL_TOPLEVEL_LANG``
are defined in this test file to set arguments to the runner's
:meth:`~cocotb.runner.build` and :meth:`~cocotb.runner.test` functions;
they are not directly handled by the runner itself.
Test filenames and functions have to follow the
`pytest discovery <https://docs.pytest.org/explanation/goodpractices.html#test-discovery>`_
convention in order to be automatically found.
By default, pytest will only show you a terse "pass/fail" information.
To see more details of the simulation run,
including the output produced by cocotb,
add the ``-s`` option to the ``pytest`` call:
.. code-block:: bash
SIM=questa HDL_TOPLEVEL_LANG=vhdl pytest examples/simple_dff/test_dff.py -s
.. note::
Take a look at the
:ref:`list of command line flags <pytest:command-line-flags>`
in pytest's official documentation.
Direct usage
=============
You can also run the test directly, that is, without using pytest, like so
.. code-block:: bash
python examples/simple_dff/test_dff.py
This requires that you define the test to run in the testcase file itself.
For example, add the following code at the end:
.. code-block:: bash
if __name__ == "__main__":
test_simple_dff_runner()
API
===
The API of the Python runner is documented in section :ref:`api-runner`.
|