# Device Test Context Classes API

The API of the testing classes are described here. For an overview of their behaviour, see Approaches to testing Tango devices.

## DeviceTestContext

class tango.test_context.DeviceTestContext(device, device_cls=None, server_name=None, instance_name=None, device_name=None, properties=None, db=None, host=None, port=0, debug=3, process=False, daemon=False, timeout=None, memorized=None, root_atts=None, green_mode=None)

  Bases: `MultiDeviceTestContext`

  Context to run a single device without a database.

  The difference with respect to `MultiDeviceTestContext` is that it only allows to export a single device.

  Example usage:

       1from time import sleep
       2
       3from tango.server import Device, attribute, command
       4from tango.test_context import DeviceTestContext
       5
       6class PowerSupply(Device):
       7
       8    @attribute(dtype=float)
       9    def voltage(self):
      10        return 1.23
      11
      12    @command
      13    def calibrate(self):
      14        sleep(0.1)
      15
      16def test_calibrate():
      17    '''Test device calibration and voltage reading.'''
      18    with DeviceTestContext(PowerSupply, process=True) as proxy:
      19        proxy.calibrate()
      20        assert proxy.voltage == 1.23

  Parameters:

    - device (`Device` or `DeviceImpl`) – Device class to be run.

    - device_cls – The device class can be provided if using the low-level API. Optional. Not required for high-level API devices, of type `Device`.

  Added in version 9.2.1.

  Added in version 9.3.3: added memorized parameter.

  Added in version 9.3.6: added green_mode parameter.

  append_db_file(server, instance, tangoclass, device_prop_info)

    Generate a database file corresponding to the given arguments.

  delete_db()

    delete temporary database file only if it was created by this class

  get_device(device_name) → DeviceProxy

    Return the device proxy corresponding to the given device name.

    Maintains previously accessed device proxies in a cache to not recreate then on every access.

  get_device_access(device_name=None) → str

    Return the full device name.

  get_server_access() → str

    Return the full server name.

  start()

    Run the server.

  stop()

    Kill the server.

## MultiDeviceTestContext

class tango.test_context.MultiDeviceTestContext(devices_info, server_name=None, instance_name=None, db=None, host=None, port=0, debug=3, process=False, daemon=False, timeout=None, green_mode=None)

  Bases: `object`

  Context to run device(s) without a database.

  The difference with respect to `DeviceTestContext` is that it allows to export multiple devices (even of different Tango classes).

  Example usage:

       1from tango import DeviceProxy
       2from tango.server import Device, attribute
       3from tango.test_context import MultiDeviceTestContext
       4
       5
       6class Device1(Device):
       7    @attribute(dtype=int)
       8    def attr1(self):
       9        return 1
      10
      11
      12class Device2(Device):
      13    @attribute(dtype=int)
      14    def attr2(self):
      15        dev1 = DeviceProxy("test/device/1")
      16        return dev1.attr1 * 2
      17
      18
      19devices_info = (
      20    {
      21        "class": Device1,
      22        "devices": [
      23            {"name": "test/device/1"},
      24        ],
      25    },
      26    {
      27        "class": Device2,
      28        "devices": [
      29            {
      30                "name": "test/device/2",
      31            },
      32        ],
      33    },
      34)
      35
      36
      37def test_devices():
      38    with MultiDeviceTestContext(devices_info, process=True) as context:
      39        proxy1 = context.get_device("test/device/1")
      40        proxy2 = context.get_device("test/device/2")
      41        assert proxy1.attr1 == 1
      42        assert proxy2.attr2 == 2

  Parameters:

    - devices_info (sequence<dict>) –

      a sequence of dicts with information about devices to be exported. Each dict consists of the following keys:

      > - ”class” which value is either of:

      >   - a : class:~tango.server.Device or the name of some such class

      >   - a sequence of two elements, the first element being a `DeviceClass` or the name of some such class, the second element being a `DeviceImpl` or the name of some such class

      > - ”devices” which value is a sequence of dicts with the following keys:

      >   - ”name” (str)

      >   - ”properties” (dict)

      >   - ”memorized” (dict)

      >   - ”root_atts” (dict)”

    - server_name (`str`) – Name to use for the device server. Optional. Default is the first device’s class name.

    - instance_name (`str`) – Name to use for the device server instance. Optional. Default is lower-case version of the server name.

    - db (`str`) – Path to a pre-populated text file to use for the database. Optional. Default is to create a new temporary file and populate it based on the devices and properties supplied in devices_info.

    - host (`str`) – Hostname to use for device server’s ORB endpoint. Optional. Default is the loopback IP address, 127.0.0.1.

    - port (`int`) – Port number to use for the device server’s ORB endpoint. Optional. Default is chosen by omniORB.

    - debug (`int`) – Debug level for the device server logging. 0=OFF, 1=FATAL, 2=ERROR, 3=WARN, 4=INFO, 5=DEBUG. Optional. Default is warn.

    - process (`bool`) – True if the device server should be launched in a new process, otherwise use a new thread. Note: if the context will be used mutiple times, it may seg fault if the thread mode is chosen. See the issues and process kwarg discussion in the docs. Optional. Default is thread.

    - daemon (`bool`) – True if the new thread/process must be created in daemon mode. Optional. Default is not daemon.

    - timeout (`float`) – How long to wait (seconds) for the device server to start up, and also how long to wait on joining the thread/process when stopping. Optional. Default differs for thread and process modes.

    - green_mode (`GreenMode`) – Green mode to use for the device server. Optional. Default uses the Device specification (via green_mode class attribute), or if that isn’t specified the global green mode.

  Added in version 9.3.2.

  Added in version 9.3.3: Added support for memorized key to “devices” field in devices_info. Added support for literal names for “class” field in devices_info.

  Added in version 9.3.3: added green_mode parameter.

  Changed in version 9.5.0: By default, devices launched by a test context can be accessed using short names with `AttributeProxy`, `DeviceProxy`, and `Group`. This can be disabled by setting the enable_test_context_tango_host_override class/instance attribute to False before starting the test context. Added support for root_atts key to “devices” field in devices_info.

  append_db_file(server, instance, tangoclass, device_prop_info)

    Generate a database file corresponding to the given arguments.

  delete_db()

    delete temporary database file only if it was created by this class

  get_device(device_name) → DeviceProxy

    Return the device proxy corresponding to the given device name.

    Maintains previously accessed device proxies in a cache to not recreate then on every access.

  get_device_access(device_name) → str

    Return the full device name.

  get_server_access() → str

    Return the full server name.

  start()

    Run the server.

  stop()

    Kill the server.