Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Unit Testing Tips and Tricks

David Perl edited this page Apr 24, 2023 · 3 revisions

Testing using ophyd.sim devices with PVs which are meant to be linked

For many real devices, such as the Zebra, or EPICS motors, we set a demand value and read back a readback value. ophyd.sim devices don't have a means of connecting PVs to emulate this, but we can do it ourselves. For example (adapted from test_zebra_setup.py - more examples there), we can replace the set method on our demand value with a Mock which ensures our readback value is updated:

from unittest.mock import MagicMock
from ophyd.status import Status

def test_zebra_arm_disarm(
    zebra: Zebra,
):
    def mock_set_armed(val: int):
        zebra.pc.armed.set(val)
        return Status(done=True, success=True)

    mock_arm_disarm = MagicMock(side_effect=mock_set_armed)
    zebra.pc.arm_demand.set = mock_arm_disarm

When something like

yield from bps.abs_set(zebra.pc.arm_demand, 1)

is executed in the RunEngine, this will ensure the value is passed on to the readback value, allowing a plan which relies on this behaviour to be simulated.

Clone this wiki locally