Skip to content

Commit 4852217

Browse files
tring
1 parent 04ca925 commit 4852217

7 files changed

Lines changed: 54 additions & 26 deletions

File tree

Keithley_2400/Backends/IV_K2400_Loop_Backend_v10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def main():
3232
Main function to run the I-V sweep measurement.
3333
"""
3434
# object creation ----------------------------------
35-
keithley_2400 = Keithley2400("GPIB::4", adapter_args={"py_library": "@sim"})
35+
keithley_2400 = Keithley2400("GPIB::4")
3636
keithley_2400.disable_buffer()
3737
sleep(2)
3838

Keithley_6517B/High_Resistance/Backends/IV_K6517B_Simple_Backend_v10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def main():
8989

9090
# --- 2. CONNECT TO INSTRUMENT (V5 Logic) ---
9191
print(f"\nAttempting to connect to instrument at: {VISA_ADDRESS}")
92-
keithley = Keithley6517B(VISA_ADDRESS, adapter_args={"py_library": "@sim"})
92+
keithley = Keithley6517B(VISA_ADDRESS)
9393
print(f"Successfully connected to: {keithley.id}")
9494

9595
# --- 3. CONFIGURE MEASUREMENT (V5 Logic) ---

tests/test_fixes.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
import unittest
22
from unittest.mock import patch, MagicMock, mock_open
3+
import pyvisa # Added import for pyvisa
34

45
class TestFixes(unittest.TestCase):
56

6-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.sleep', MagicMock()) # Add mock for time.sleep to isolate the test
7+
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.sleep', MagicMock())
78
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
8-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
9+
@patch('pyvisa.ResourceManager') # Patch pyvisa.ResourceManager
910
@patch('matplotlib.pyplot.show')
1011
@patch('pandas.DataFrame.to_csv')
11-
def test_iv_k2400_fix(self, mock_to_csv, mock_plt_show, mock_keithley_class, mock_input):
12+
def test_iv_k2400_fix(self, mock_to_csv, mock_plt_show, mock_rm, mock_input):
1213
"""
1314
This test verifies that the Keithley2400 is correctly mocked in the
1415
IV_K2400_Loop_Backend_v10 script, preventing real hardware calls.
1516
"""
1617
from Keithley_2400.Backends import IV_K2400_Loop_Backend_v10 as iv_backend
18+
19+
mock_keithley_instance = MagicMock()
20+
mock_rm.return_value.open_resource.return_value = mock_keithley_instance
21+
mock_keithley_instance.query.return_value = "KEITHLEY INSTRUMENTS INC., MODEL 2400" # Simulate IDN query
22+
1723
iv_backend.main()
18-
mock_keithley_class.assert_called_once()
24+
mock_rm.return_value.open_resource.assert_called_once_with("GPIB::4")
25+
1926

2027
@patch('tkinter.Tk')
2128
@patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv')
2229
@patch('builtins.input', side_effect=['10', '20', '5', '30'])
23-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350')
30+
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.pyvisa.ResourceManager') # Patch pyvisa.ResourceManager
2431
@patch('matplotlib.pyplot.show')
2532
@patch('builtins.open', new_callable=mock_open)
2633
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.time.sleep', MagicMock())
2734
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
28-
def test_t_control_l350_fix(self, mock_time, mock_open_file,
29-
mock_plt_show, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
35+
def test_t_control_l350_fix(self, mock_time, mock_open_file, mock_plt_show, mock_rm, mock_input, mock_file_dialog, mock_tk):
3036
from Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10 import main
31-
main()
32-
mock_ls_class.assert_called_once()
3337

34-
if __name__ == '__main__':
35-
unittest.main()
38+
mock_instrument_from_rm = MagicMock()
39+
mock_rm.return_value.open_resource.return_value = mock_instrument_from_rm
40+
mock_instrument_from_rm.query.return_value = "LSCI,MODEL350,12345,1.0"
41+
42+
main()
43+
mock_rm.return_value.open_resource.assert_called_once_with("GPIB0::13::INSTR")

tests/test_gui_iv_k2400.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_start_measurement_logic(self, mock_fig_subplots, MockBackend):
3333
# Configure the mock for subplots to return two mock axes
3434
mock_ax_vi = MagicMock()
3535
mock_ax_ri = MagicMock()
36-
mock_fig_subplots.return_value = (mock_ax_vi, mock_ax_ri)
36+
mock_fig_subplots.return_value = [mock_ax_vi, mock_ax_ri]
3737

3838
# Instantiate the GUI. This also creates all the tk widgets.
3939
app = MeasurementAppGUI(self.root)

tests/test_iv_k2400_loop_backend.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from unittest.mock import patch, MagicMock
3+
import pyvisa
34

45

56
# Now we can import the module to be tested
@@ -9,7 +10,7 @@
910
class TestIVK2400LoopBackend(unittest.TestCase):
1011

1112
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
12-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
13+
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400') # Patch Keithley2400 directly
1314
@patch('matplotlib.pyplot.show')
1415
@patch('pandas.DataFrame.to_csv')
1516
def test_main_full_run(self, mock_to_csv, mock_plt_show, mock_keithley_class, mock_input):
@@ -31,6 +32,16 @@ def voltage_side_effect():
3132
# so we will use a side effect to track it
3233
latest_current = [0]
3334

35+
# Simulate the voltage measurement
36+
# Let's return a voltage that is proportional to the current
37+
def voltage_side_effect():
38+
# A simple linear relationship for testing
39+
return mock_keithley_instance.source_current * 10
40+
41+
# We can't directly access the source_current from ramp_to_current,
42+
# so we will use a side effect to track it
43+
latest_current = [0]
44+
3445
def ramp_side_effect(current):
3546
latest_current[0] = current
3647
# Also update the mock's internal state
@@ -47,7 +58,7 @@ def ramp_side_effect(current):
4758

4859
# --- ASSERTIONS ---
4960
# 1. Check instrument initialization
50-
mock_keithley_class.assert_called_with("GPIB::4", adapter_args={"py_library": "@sim"})
61+
mock_keithley_class.assert_called_once_with("GPIB::4")
5162
mock_keithley_instance.disable_buffer.assert_called_once()
5263

5364
# 2. Check instrument configuration

tests/test_iv_k6517b_simple_backend.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import unittest
22
from unittest.mock import patch, MagicMock, mock_open
33
import numpy as np
4+
# import pyvisa # Removed pyvisa import as it's not needed with direct class patching
45

56
# Import the main function from the script we want to test
67
from Keithley_6517B.High_Resistance.Backends.IV_K6517B_Simple_Backend_v10 import main as iv_simple_main
78

89
class TestIVK6517BSimpleBackend(unittest.TestCase):
910
@patch('time.sleep', MagicMock())
1011
@patch('builtins.input', side_effect=['-10', '10', '5', '0.1', 'test_iv_simple.csv'])
11-
@patch('Keithley_6517B.High_Resistance.Backends.IV_K6517B_Simple_Backend_v10.Keithley6517B')
12+
@patch('Keithley_6517B.High_Resistance.Backends.IV_K6517B_Simple_Backend_v10.Keithley6517B') # Re-patch Keithley6517B directly
1213
@patch('builtins.open', new_callable=mock_open)
1314
@patch('matplotlib.pyplot.show')
1415
def test_full_run(self, mock_show, mock_file, mock_keithley_class, mock_input):
@@ -17,11 +18,12 @@ def test_full_run(self, mock_show, mock_file, mock_keithley_class, mock_input):
1718
"""
1819
# --- Setup Mocks ---
1920
mock_instrument = MagicMock()
21+
mock_keithley_class.return_value = mock_instrument
2022
# Set a mock ID for the connection message
2123
mock_instrument.id = "Mocked Keithley 6517B"
2224
# Simulate resistance measurement
2325
mock_instrument.resistance = 1.23e9
24-
mock_keithley_class.return_value = mock_instrument
26+
2527

2628
# --- Run the main function and catch exceptions ---
2729
try:
@@ -33,7 +35,7 @@ def test_full_run(self, mock_show, mock_file, mock_keithley_class, mock_input):
3335

3436
# --- Assertions ---
3537
# 1. Was the instrument initialized correctly?
36-
mock_keithley_class.assert_called_once_with("GPIB1::27::INSTR", adapter_args={"py_library": "@sim"})
38+
mock_keithley_class.assert_called_once_with("GPIB1::27::INSTR")
3739

3840
# 2. Was the zero-check and correction sequence performed?
3941
mock_instrument.reset.assert_called_once()

tests/test_t_control_l350_backend.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,29 @@ def test_get_user_parameters(self, mock_input):
9494
@patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv')
9595
@patch('builtins.input', side_effect=['10', '20', '5', '30'])
9696
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350')
97+
@patch('matplotlib.pyplot.subplots') # NEW PATCH
9798
@patch('matplotlib.pyplot.show')
9899
@patch('builtins.open', new_callable=mock_open)
99100
@patch('time.sleep', MagicMock())
100101
# Simulate time passing
101102
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
102-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.pyvisa.ResourceManager')
103-
def test_main_runs_and_completes(self, mock_rm, mock_time, mock_open_file,
104-
mock_plt_show, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
103+
def test_main_runs_and_completes(self, mock_time, mock_open_file,
104+
mock_plt_show, mock_plt_subplots, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
105105
# --- MOCK SETUP ---
106-
# Configure mock_rm to return a mock instrument
106+
mock_controller = MagicMock()
107+
mock_ls_class.return_value = mock_controller
108+
109+
# Configure the internal pyvisa.ResourceManager within the mocked Lakeshore350 instance
110+
mock_rm_inside_ls350 = MagicMock()
111+
mock_controller.rm = mock_rm_inside_ls350 # lakeshore350 creates rm = pyvisa.ResourceManager()
107112
mock_instrument_from_rm = MagicMock()
108-
mock_rm.return_value.open_resource.return_value = mock_instrument_from_rm
113+
mock_rm_inside_ls350.open_resource.return_value = mock_instrument_from_rm
109114
mock_instrument_from_rm.query.return_value = "LSCI,MODEL350,12345,1.0" # IDN query
110115

111-
mock_controller = MagicMock()
112-
mock_ls_class.return_value = mock_controller
116+
# Configure mock_plt_subplots
117+
mock_fig = MagicMock()
118+
mock_ax = MagicMock()
119+
mock_plt_subplots.return_value = (mock_fig, mock_ax)
113120

114121
# Simulate temperature readings to control the loop
115122
# Start at 10K, then ramp up to 21K to finish

0 commit comments

Comments
 (0)