# 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)

  Bases: `tango.test_context.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`.

  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)

    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)

    Return the full device name.

  get_server_access()

    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)

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

  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:

      >   - : 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 such such class

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

      >   - ”name” (str)

      >   - ”properties” (dict)

      >   - ”memorized” (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 a local IP address.

    - 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. 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.

  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)

    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)

    Return the full device name.

  get_server_access()

    Return the full server name.

  start()

    Run the server.

  stop()

    Kill the server.