Skip to content

Commit

Permalink
Tests and some BFs for HP devices (#284)
Browse files Browse the repository at this point in the history
Full cov test suites for:
- HP3456a
- HP6624a
- HP6632b
- HP6652a
- HPe3631a

Bug fix:
- HPe3631a: Raise an error that was previously returned

Changes:
- HP3456a: Remove `if self._testing` statement to not do `time.sleep`
  during tests. Instead use an automatically used pytest fixture to
  mock out `time.sleep`.
  • Loading branch information
trappitsch committed Nov 5, 2020
1 parent 9865a69 commit c739ca0
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 13 deletions.
6 changes: 2 additions & 4 deletions instruments/hp/hp3456a.py
Expand Up @@ -574,8 +574,7 @@ def _register_read(self, name):
"HP3456a.Register, got {} "
"instead.".format(name))
self.sendcmd("RE{}".format(name.value))
if not self._testing: # pragma: no cover
time.sleep(.1)
time.sleep(.1)
return float(self.query("", size=-1))

def _register_write(self, name, value):
Expand All @@ -601,8 +600,7 @@ def _register_write(self, name, value):
]:
raise ValueError("register {} is read only".format(name))
self.sendcmd("W{}ST{}".format(value, name.value))
if not self._testing: # pragma: no cover
time.sleep(.1)
time.sleep(.1)

def trigger(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion instruments/hp/hpe3631a.py
Expand Up @@ -141,7 +141,7 @@ def mode(self):
is lower than I. If the load is smaller than V/I, the set current
I acts as a current limiter and the voltage is lower than V.
"""
return AttributeError("The `HPe3631a` sets its mode automatically")
raise AttributeError("The `HPe3631a` sets its mode automatically")

channelid = int_property(
"INST:NSEL",
Expand Down
9 changes: 9 additions & 0 deletions instruments/tests/test_hp/test_hp3456a.py
Expand Up @@ -6,6 +6,7 @@

# IMPORTS #####################################################################

import time

import pytest

Expand All @@ -18,6 +19,12 @@
# pylint: disable=protected-access


@pytest.fixture(autouse=True)
def time_mock(mocker):
"""Mock out time to speed up."""
return mocker.patch.object(time, 'sleep', return_value=None)


def test_hp3456a_trigger_mode():
with expected_protocol(
ik.hp.HP3456a,
Expand Down Expand Up @@ -338,6 +345,8 @@ def test_hp3456a_input_range():
) as dmm:
dmm.input_range = 10 ** -1 * u.volt
dmm.input_range = 1e3 * u.ohm
with pytest.raises(NotImplementedError):
_ = dmm.input_range


def test_hp3456a_input_range_invalid_str():
Expand Down
15 changes: 15 additions & 0 deletions instruments/tests/test_hp/test_hp6624a.py
Expand Up @@ -57,6 +57,21 @@ def test_channel_query():
assert value == "FOO"


def test_mode():
"""Raise NotImplementedError when mode is called."""
with expected_protocol(
ik.hp.HP6624a,
[],
[],
sep="\n"
) as hp:
channel = hp.channel[0]
with pytest.raises(NotImplementedError):
_ = channel.mode
with pytest.raises(NotImplementedError):
channel.mode = 42


def test_channel_voltage():
with expected_protocol(
ik.hp.HP6624a,
Expand Down
40 changes: 40 additions & 0 deletions instruments/tests/test_hp/test_hp6632b.py
Expand Up @@ -6,6 +6,7 @@

# IMPORTS #####################################################################

import pytest

from instruments.units import ureg as u

Expand Down Expand Up @@ -326,6 +327,45 @@ def test_hp6632b_abort_output_trigger():
psu.abort_output_trigger()


def test_line_frequency():
"""Raise NotImplemented error when called."""
with expected_protocol(
ik.hp.HP6632b,
[],
[]
) as psu:
with pytest.raises(NotImplementedError):
psu.line_frequency = 42
with pytest.raises(NotImplementedError):
_ = psu.line_frequency


def test_display_brightness():
"""Raise NotImplemented error when called."""
with expected_protocol(
ik.hp.HP6632b,
[],
[]
) as psu:
with pytest.raises(NotImplementedError):
psu.display_brightness = 42
with pytest.raises(NotImplementedError):
_ = psu.display_brightness


def test_display_contrast():
"""Raise NotImplemented error when called."""
with expected_protocol(
ik.hp.HP6632b,
[],
[]
) as psu:
with pytest.raises(NotImplementedError):
psu.display_contrast = 42
with pytest.raises(NotImplementedError):
_ = psu.display_contrast


def test_hp6632b_check_error_queue():
with expected_protocol(
ik.hp.HP6632b,
Expand Down
16 changes: 16 additions & 0 deletions instruments/tests/test_hp/test_hp6652a.py
Expand Up @@ -6,6 +6,7 @@

# IMPORTS #####################################################################

import pytest

import instruments as ik
from instruments.tests import expected_protocol
Expand All @@ -27,6 +28,21 @@ def test_name():
assert hp.name == "FOO BAR"


def test_mode():
"""Raise NotImplementedError when called."""
with expected_protocol(
ik.hp.HP6652a,
[
],
[
],
sep="\n"
) as hp:
with pytest.raises(NotImplementedError):
_ = hp.mode
with pytest.raises(NotImplementedError):
hp.mode = 42

def test_reset():
with expected_protocol(
ik.hp.HP6652a,
Expand Down
64 changes: 56 additions & 8 deletions instruments/tests/test_hp/test_hpe3631a.py
Expand Up @@ -6,6 +6,9 @@

# IMPORTS #####################################################################

import time

import pytest

from instruments.units import ureg as u

Expand All @@ -15,6 +18,12 @@

# TESTS #######################################################################

@pytest.fixture(autouse=True)
def time_mock(mocker):
"""Mock out time such that the tests go faster."""
return mocker.patch.object(time, 'sleep', return_value=None)


def test_channel():
with expected_protocol(
ik.hp.HPe3631a,
Expand All @@ -34,6 +43,7 @@ def test_channel():
assert inst.channelid == 1
assert inst.channel[2] == inst
assert inst.channelid == 2
assert inst.channel.__len__() == len([1, 2, 3]) # len of valild set


def test_channelid():
Expand All @@ -55,6 +65,22 @@ def test_channelid():
assert inst.channelid == 2


def test_mode():
"""Raise AttributeError since instrument sets mode automatically."""
with expected_protocol(
ik.hp.HPe3631a,
[
"SYST:REM"
],
[
]
) as inst:
with pytest.raises(AttributeError) as err_info:
_ = inst.mode()
err_msg = err_info.value.args[0]
assert err_msg == "The `HPe3631a` sets its mode automatically"


def test_voltage():
with expected_protocol(
ik.hp.HPe3631a,
Expand All @@ -81,14 +107,36 @@ def test_voltage():
assert inst.voltage_max == 6.0 * u.volt
inst.voltage = 3.0 * u.volt
assert inst.voltage == 3.0 * u.volt
try:
inst.voltage = -1.0 * u.volt
except ValueError:
pass
try:
inst.voltage = 7.0 * u.volt
except ValueError:
pass
with pytest.raises(ValueError) as err_info:
newval = -1.0 * u.volt
inst.voltage = newval
err_msg = err_info.value.args[0]
assert err_msg == f"Voltage quantity is too low. Got {newval}, " \
f"minimum value is {0.}"
with pytest.raises(ValueError) as err_info:
newval = 7.0 * u.volt
inst.voltage = newval
err_msg = err_info.value.args[0]
assert err_msg == f"Voltage quantity is too high. Got {newval}, " \
f"maximum value is {u.Quantity(6.0, u.V)}"


def test_voltage_range_negative():
"""Get voltage max if negative."""
max_volts = -6.
with expected_protocol(
ik.hp.HPe3631a,
[
"SYST:REM", # 0
"SOUR:VOLT? MAX" # 1
],
[
f"{max_volts}", # 1
]
) as inst:
expected_value = u.Quantity(max_volts, u.V), 0.
received_value = inst.voltage_range
assert expected_value == received_value


def test_current():
Expand Down

0 comments on commit c739ca0

Please sign in to comment.