Pytest Plugin Reference#
Warning
The pytest plugin is under active development and the API will change in breaking ways over the next release or two.
You can still use pytest with the Python runners for building designs and running simulations,
and you can also still use pytest features in cocotb tests, such pytest.raises(), pytest.skip(), or pytest’s assertion rewriting.
Fixtures#
- fixture cocotb_tools._pytest.plugin.dut[source]#
Scope: session
A cocotb fixture that is providing a simulation handle to DUT.
Provided simulation handle is the same as
cocotb.top.Example usage:
async def test_dut_feature_1(dut) -> None: dut.rst.value = 0 dut.clk.value = 0 # Testing DUT feature ...
- Returns:
Simulation handle to DUT (equivalent to
cocotb.top).
- fixture cocotb_tools._pytest.plugin.hdl_session[source]#
Scope: session
A cocotb fixture that is providing a helper instance to define own HDL design and to build it.
Note
This fixture is scoped to global
sessionscope. It can be useful to build the whole HDL project with different HDL modules at once not per test.It contains own instance of
Runnerthat can be accessed directly fromrunnermember.Defined HDL design can be build by invoking the
build()method from non-async test functions. This method will invoke build step fromrunnermember.Requested fixture will pass various plugin options to own instance of
Runner. Like setting a desired verbosity level for cocotb runner.Please refer to available public members of
HDLthat can be used to define own HDL design.Example usage:
import pytest from cocotb_tools._pytest.hdl import HDL @pytest.fixture(scope="session") def my_hdl_project(hdl_session: HDL) -> HDL: # Build whole HDL design with all HDL modules at once hdl_session.sources = ( # Add more HDL source files here # ... DIR / "my_hdl_module_1.sv", DIR / "my_hdl_module_2.sv", ) hdl_session.build() return hdl_session @pytest.fixture(name="my_hdl_module_1") def my_hdl_module_1_fixture(hdl: HDL, my_hdl_project: HDL) -> HDL: # Define HDL module 1 hdl.build_dir = my_hdl_project.build_dir hdl.toplevel = "my_hdl_module_1" return hdl @pytest.fixture(name="my_hdl_module_2") def my_hdl_module_2_fixture(hdl: HDL, my_hdl_project: HDL) -> HDL: # Define HDL module 2 hdl.build_dir = my_hdl_project.build_dir hdl.toplevel = "my_hdl_module_2" return hdl @pytest.mark.cocotb_runner def test_dut_1(my_hdl_module_1: HDL) -> None: # Run HDL simulator with cocotb tests my_hdl_module_1.test() @pytest.mark.cocotb_runner def test_dut_2(my_hdl_module_2: HDL) -> None: # Run HDL simulator with cocotb tests my_hdl_module_2.test()
- fixture cocotb_tools._pytest.plugin.hdl[source]#
Scope: function
A cocotb fixture that is providing a helper instance to define own HDL design, to build it and run set of cocotb tests from test modules (testbenches) against selected top level design.
Note
This fixture is scoped to default
functionscope. It can help to build HDL module per test and run tests for defined HDL top level.It contains own instance of
Runnerthat can be accessed directly fromrunnermember.Defined HDL design can be build by invoking the
build()method and test by invoking thetest()method from non-async test functions. These methods will invoke build and test steps fromrunnermember.Requested fixture will pass various plugin options to own instance of
Runner. Like setting a desired verbosity level for cocotb runner.Please refer to available public members of
HDLthat can be used to define own HDL design.Example usage:
import pytest from cocotb_tools._pytest.hdl import HDL @pytest.fixture(name="my_hdl_module") def my_hdl_module_fixture(hdl: HDL) -> HDL: # Build HDL module per test hdl.toplevel = "my_hdl_module" hdl.sources = ( # Add more HDL source files here # ... DIR / "my_hdl_module.sv", ) hdl.build() return hdl @pytest.mark.cocotb_runner def test_dut(my_hdl_module: HDL) -> None: # Run HDL simulator with cocotb tests my_hdl_module.test()
Markers#
- @cocotb_tools._pytest.mark.cocotb_runner( ) MarkDecorator[source]#
Mark test function as cocotb runner.
Example usage:
import pytest from cocotb_tools._pytest.hdl import HDL @pytest.fixture(name="sample_module") def sample_module_fixture(hdl: HDL) -> HDL: # Define HDL design and build it hdl.toplevel = "sample_module" hdl.sources = (DESIGNS / "sample_module.sv",) hdl.build() return hdl @pytest.mark.cocotb_runner def test_dut(sample_module: HDL) -> None: # Run HDL simulator with cocotb tests sample_module.test()
- Parameters:
test_module – Name of Python module with cocotb tests to be loaded by cocotb
runner.- Returns:
Decorated test function as cocotb runner.
- @cocotb_tools._pytest.mark.cocotb_test[source]#
Mark coroutine function as cocotb test.
Example usage:
# NOTE: decorator is not needed if coroutine function starts with the test_ prefix and it uses dut fixture async def test_dut_feature(dut) -> None: # Test DUT feature ... @pytest.mark.cocotb_test async def non_canonical_test_name(dut) -> None: # Test DUT feature but from a test function that doesn't follow with the pytest naming convention ...
- Returns:
Decorated coroutine function as cocotb test.
- @cocotb_tools._pytest.mark.cocotb_timeout(
- duration: float,
- unit: 'step' | 'fs' | 'ps' | 'ns' | 'us' | 'ms' | 'sec',
Mark coroutine function with simulation time duration before the test is forced to fail.
Example usage:
@pytest.mark.cocotb_timeout(duration=200, unit="ns") async def test_dut_feature_with_timeout(dut) -> None: # Test DUT feature with timeout configured from cocotb marker ...
- Parameters:
duration – Simulation time duration before the test is forced to fail.
unit – Simulation time unit that accepts any unit that
Timerdoes.
- Raises:
SimTimeoutError – Test function timeouted.
- Returns:
Decorated coroutine function with simulation time duration before the test is forced to fail.
- @cocotb_tools._pytest.mark.cocotb_sources[source]#
Add language-agnostic list of source files to build.
- @cocotb_tools._pytest.mark.cocotb_parameters[source]#
Set Verilog/SystemVerilog parameters and VHDL generics.
- @cocotb_tools._pytest.mark.cocotb_timescale( ) MarkDecorator[source]#
Set time unit and time precision for simulation.
HDL Fixture Request#
- class cocotb_tools._pytest.hdl.HDL(request: FixtureRequest)[source]#
Build HDL design and run test against specific HDL top level.
- build(
- library: str | None = None,
- sources: Sequence[PathLike[str] | str | VHDL | Verilog | VerilatorControlFile] | None = None,
- includes: Sequence[PathLike[str] | str] | None = None,
- defines: Mapping[str, object] | None = None,
- parameters: Mapping[str, object] | None = None,
- build_args: Sequence[str | VHDL | Verilog] | None = None,
- toplevel: str | None = None,
- always: bool = False,
- clean: bool = False,
- verbose: bool = False,
- timescale: tuple[str, str] | None = None,
- waves: bool = False,
- build_dir: PathLike[str] | str | None = None,
- cwd: PathLike[str] | str | None = None,
Build HDL design.
- Parameters:
library – The library name to compile into.
sources – Language-agnostic list of source files to build.
includes – Verilog include directories.
defines – Defines to set.
parameters – Verilog parameters or VHDL generics.
build_args – Extra build arguments for the simulator.
toplevel – Name of the HDL toplevel module.
always – Always run the build step.
clean – Delete build_dir before building.
verbose – Enable verbose messages.
timescale – Tuple containing time unit and time precision for simulation.
waves – Record signal traces.
build_dir – Directory to run the build step in.
cwd – Directory to execute the build command(s) in.
- build_args: MutableSequence[str | VHDL | Verilog]#
Extra build arguments for the simulator.
- defines: MutableMapping[str, object]#
Defines to set.
- elab_args: MutableSequence[str]#
A list of elaboration arguments for the simulator.
- env: MutableMapping[str, str]#
Extra environment variables to set.
- includes: MutableSequence[PathLike[str] | str]#
Verilog include directories.
- parameters: MutableMapping[str, object]#
Verilog parameters or VHDL generics.
- plusargs: MutableSequence[str]#
‘plusargs’ to set for the simulator.
- pre_cmd: list[str]#
Commands to run before simulation begins. Typically Tcl commands for simulators that support them.
- sources: MutableSequence[PathLike[str] | str | VHDL | Verilog | VerilatorControlFile]#
Language-agnostic list of source files to build.
- test(
- test_module: str | Sequence[str] | None = None,
- toplevel: str | None = None,
- toplevel_library: str | None = None,
- toplevel_lang: str | None = None,
- gpi_interfaces: list[str] | None = None,
- parameters: Mapping[str, object] | None = None,
- seed: str | int | None = None,
- elab_args: Sequence[str] | None = None,
- test_args: Sequence[str] | None = None,
- plusargs: Sequence[str] | None = None,
- env: Mapping[str, str] | None = None,
- gui: bool = False,
- waves: bool = False,
- verbose: bool = False,
- pre_cmd: list[str] | None = None,
- timescale: tuple[str, str] | None = None,
- build_dir: PathLike[str] | str | None = None,
- test_dir: PathLike[str] | str | None = None,
Test HDL design.
- Parameters:
test_module – Name(s) of the Python module(s) containing the tests to run.
toplevel – Name of the HDL toplevel module.
toplevel_library – The library name for HDL toplevel module.
toplevel_lang – Language of the HDL toplevel module.
gpi_interfaces – List of GPI interfaces to use, with the first one being the entry point.
parameters – Verilog parameters or VHDL generics.
seed – A specific random seed to use.
elab_args – A list of elaboration arguments for the simulator.
test_args – A list of extra arguments for the simulator.
plusargs – ‘plusargs’ to set for the simulator.
env – Extra environment variables to set.
gui – Run with simulator GUI.
waves – Record signal traces.
verbose – Enable verbose messages.
pre_cmd – Commands to run before simulation begins. Typically Tcl commands for simulators that support them.
timescale – Tuple containing time unit and time precision for simulation.
build_dir – Directory to run the build step in.
test_dir – Directory to run the tests in.
- Returns:
Path to created results file with cocotb tests in JUnit XML format.
- test_args: MutableSequence[str]#
A list of extra arguments for the simulator.
Hook Specifications#
- cocotb_tools._pytest.hookspecs.pytest_cocotb_make_hdl(
- request: FixtureRequest,
Create new instance of
cocotb_tools._pytest.hdl.HDL.Note
Any conftest file can implement this hook. Stops at first non-None result.
- Parameters:
request – The pytest fixture request object.
- Returns:
New instance of HDL.
- cocotb_tools._pytest.hookspecs.pytest_cocotb_make_runner(
- simulator_name: str,
Create new instance of
cocotb_tools.runner.Runner.Note
Any conftest file can implement this hook. Stops at first non-None result.
- Parameters:
simulator_name – Name of HDL simulator.
- Returns:
New instance of runner.