Skip to content

Commit b116a4e

Browse files
update
1 parent 6c5b032 commit b116a4e

2 files changed

Lines changed: 30 additions & 52 deletions

File tree

tests/test_iv_k2400_loop_backend.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,19 @@
33

44

55
# Now we can import the module to be tested
6-
# from Keithley_2400.Backends import IV_K2400_Loop_Backend_v10 as iv_backend
6+
from Keithley_2400.Backends import IV_K2400_Loop_Backend_v10 as iv_backend
77

88

99
class TestIVK2400LoopBackend(unittest.TestCase):
1010

1111
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
12-
@patch('pymeasure.instruments.keithley.Keithley2400')
12+
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
1313
@patch('matplotlib.pyplot.show')
1414
@patch('pandas.DataFrame.to_csv')
1515
def test_main_full_run(self, mock_to_csv, mock_plt_show, mock_keithley_class, mock_input):
1616
"""
1717
Test the main function to ensure it runs through the full I-V sweep process.
1818
"""
19-
# --- DYNAMIC IMPORT ---
20-
# Import the module here so that the patches are in effect
21-
from Keithley_2400.Backends import IV_K2400_Loop_Backend_v10 as iv_backend
22-
2319
# --- MOCK SETUP ---
2420
# Mock the instrument instance
2521
mock_keithley_instance = MagicMock()
@@ -66,7 +62,7 @@ def ramp_side_effect(current):
6662
# np.linspace(0, 10, int(10/2) + 1) -> linspace(0, 10, 6)
6763
# The values will be [0., 2., 4., 6., 8., 10.]
6864
expected_currents_uA = [0., 2., 4., 6., 8., 10.]
69-
expected_.calls = [unittest.mock.call(
65+
expected_calls = [unittest.mock.call(
7066
c * 1e-6) for c in expected_currents_uA]
7167
mock_keithley_instance.ramp_to_current.assert_has_calls(
7268
expected_calls)

tests/test_t_control_l350_backend.py

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import pyvisa
44

55

6-
76
from Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10 import (
8-
Lakeshore350, get_user_parameters)
7+
Lakeshore350, get_user_parameters, main)
98

109

1110
class TestLakeshore350Class(unittest.TestCase):
@@ -16,21 +15,15 @@ def setUp(self, mock_rm):
1615
mock_rm.return_value.open_resource.return_value = self.mock_instrument
1716
self.mock_instrument.query.return_value = "LSCI,MODEL350,12345,1.0"
1817

19-
try:
20-
# Instantiate the class, which will now use the mock instrument
21-
self.controller = Lakeshore350("GPIB0::13::INSTR")
22-
# Keep a reference to the mock for assertions
23-
self.controller.instrument = self.mock_instrument
24-
except (ConnectionError, ValueError):
25-
# If the pyvisa backend is not installed, we can't create the real instrument
26-
# In that case, we will create a mock object for the tests to pass
27-
self.controller = MagicMock()
18+
# Instantiate the class, which will now use the mock instrument
19+
self.controller = Lakeshore350("GPIB0::13::INSTR")
20+
# Keep a reference to the mock for assertions
21+
self.controller.instrument = self.mock_instrument
2822

2923
def test_initialization_success(self):
3024
# Test that the instrument is initialized and queried
31-
if self.controller.instrument:
32-
self.mock_instrument.query.assert_called_with('*IDN?')
33-
self.assertIsNotNone(self.controller.instrument)
25+
self.mock_instrument.query.assert_called_with('*IDN?')
26+
self.assertIsNotNone(self.controller.instrument)
3427

3528
@patch('pyvisa.ResourceManager')
3629
def test_initialization_failure(self, mock_rm):
@@ -43,53 +36,45 @@ def test_initialization_failure(self, mock_rm):
4336
def test_reset_and_clear(self):
4437
with patch('time.sleep') as mock_sleep:
4538
self.controller.reset_and_clear()
46-
if self.controller.instrument:
47-
self.mock_instrument.write.assert_any_call('*RST')
48-
self.mock_instrument.write.assert_any_call('*CLS')
49-
self.assertEqual(mock_sleep.call_count, 2)
39+
self.mock_instrument.write.assert_any_call('*RST')
40+
self.mock_instrument.write.assert_any_call('*CLS')
41+
self.assertEqual(mock_sleep.call_count, 2)
5042

5143
def test_setup_heater(self):
5244
self.controller.setup_heater(1, 1, 2)
53-
if self.controller.instrument:
54-
self.mock_instrument.write.assert_called_with('HTRSET 1,1,2,0,1')
45+
self.mock_instrument.write.assert_called_with('HTRSET 1,1,2,0,1')
5546

5647
def test_setup_ramp(self):
5748
self.controller.setup_ramp(1, 10.0, ramp_on=True)
58-
if self.controller.instrument:
59-
self.mock_instrument.write.assert_called_with('RAMP 1,1,10.0')
49+
self.mock_instrument.write.assert_called_with('RAMP 1,1,10.0')
6050

6151
def test_set_setpoint(self):
6252
self.controller.set_setpoint(1, 150.0)
63-
if self.controller.instrument:
64-
self.mock_instrument.write.assert_called_with('SETP 1,150.0')
53+
self.mock_instrument.write.assert_called_with('SETP 1,150.0')
6554

6655
def test_set_heater_range(self):
6756
self.controller.set_heater_range(1, 'high')
68-
if self.controller.instrument:
69-
self.mock_instrument.write.assert_called_with('RANGE 1,5')
57+
self.mock_instrument.write.assert_called_with('RANGE 1,5')
7058

7159
def test_get_temperature(self):
72-
if self.controller.instrument:
73-
self.mock_instrument.query.return_value = "300.123"
74-
temp = self.controller.get_temperature('A')
75-
self.mock_instrument.query.assert_called_with('KRDG? A')
76-
self.assertAlmostEqual(temp, 300.123)
60+
self.mock_instrument.query.return_value = "300.123"
61+
temp = self.controller.get_temperature('A')
62+
self.mock_instrument.query.assert_called_with('KRDG? A')
63+
self.assertAlmostEqual(temp, 300.123)
7764

7865
def test_get_heater_output(self):
79-
if self.controller.instrument:
80-
self.mock_instrument.query.return_value = "50.5"
81-
output = self.controller.get_heater_output(1)
82-
self.mock_instrument.query.assert_called_with('HTR? 1')
83-
self.assertAlmostEqual(output, 50.5)
66+
self.mock_instrument.query.return_value = "50.5"
67+
output = self.controller.get_heater_output(1)
68+
self.mock_instrument.query.assert_called_with('HTR? 1')
69+
self.assertAlmostEqual(output, 50.5)
8470

8571
def test_close(self):
8672
self.controller.close()
87-
if self.controller.instrument:
88-
# Checks that heater is turned off
89-
self.mock_instrument.write.assert_called_with('RANGE 1,0')
90-
# Checks that the instrument connection is closed
91-
self.mock_instrument.close.assert_called_once()
92-
self.assertIsNone(self.controller.instrument)
73+
# Checks that heater is turned off
74+
self.mock_instrument.write.assert_called_with('RANGE 1,0')
75+
# Checks that the instrument connection is closed
76+
self.mock_instrument.close.assert_called_once()
77+
self.assertIsNone(self.controller.instrument)
9378

9479

9580
class TestMainFunctionAndUserInput(unittest.TestCase):
@@ -117,9 +102,6 @@ def test_get_user_parameters(self, mock_input):
117102
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
118103
def test_main_runs_and_completes(self, mock_time, mock_open_file,
119104
mock_plt_show, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
120-
# --- DYNAMIC IMPORT ---
121-
from Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10 import main
122-
123105
# --- MOCK SETUP ---
124106
mock_controller = MagicMock()
125107
mock_ls_class.return_value = mock_controller

0 commit comments

Comments
 (0)