Skip to content

Commit df4e7c9

Browse files
flask8 fixes 1
1 parent 4f00c2c commit df4e7c9

5 files changed

Lines changed: 87 additions & 39 deletions

tests/test_fixes.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class TestFixes(unittest.TestCase):
66

7-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.sleep', MagicMock())
7+
@patch('time.sleep', MagicMock())
88
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
99
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
1010
@patch('matplotlib.pyplot.show')
@@ -23,27 +23,22 @@ def test_iv_k2400_fix(self, mock_to_csv, mock_plt_show, mock_keithley_class, moc
2323
iv_backend.main()
2424
mock_keithley_class.assert_called_once_with("GPIB::4")
2525

26-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.pyvisa.ResourceManager')
2726
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350')
28-
def test_t_control_l350_fix(self, mock_ls_class, mock_rm):
27+
def test_t_control_l350_fix(self, mock_ls_class):
2928
from Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10 import main
3029

31-
# Mock pyvisa.ResourceManager and its open_resource method
32-
mock_resource_manager_instance = MagicMock()
33-
mock_rm.return_value = mock_resource_manager_instance
34-
mock_instrument = MagicMock()
35-
mock_resource_manager_instance.open_resource.return_value = mock_instrument
36-
mock_instrument.query.return_value = "Mocked Lakeshore 350 ID" # For IDN query
37-
3830
# Configure the mock Lakeshore350 instance that main() will receive
3931
mock_controller_instance = MagicMock()
4032
mock_ls_class.return_value = mock_controller_instance
41-
mock_controller_instance.get_temperature.side_effect = [10.0, 10.0, 10.0, 15.0, 21.0] # Simulate temp increase
42-
mock_controller_instance.get_heater_output.return_value = 25.0 # Simulate heater output
33+
# Simulate temp increase
34+
mock_controller_instance.get_temperature.side_effect = [
35+
10.0, 10.0, 10.0, 15.0, 21.0]
36+
mock_controller_instance.get_heater_output.return_value = 25.0 # Simulate heater output
4337

4438
with patch('builtins.input', side_effect=['10', '20', '5', '30']), \
4539
patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv'), \
4640
patch('matplotlib.pyplot.show'), \
4741
patch('builtins.open', mock_open()):
4842
main()
49-
mock_ls_class.assert_called_once_with("GPIB0::13::INSTR")
43+
mock_ls_class.assert_called_once_with(
44+
"GPIB0::13::INSTR", adapter_args={'py_library': '@sim'})

tests/test_gui_iv_k2400.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ def tearDown(self):
2424
self.root.destroy()
2525

2626
@patch('Keithley_2400.IV_K2400_GUI_v5.Keithley2400_IV_Backend')
27-
@patch('matplotlib.figure.Figure.subplots', return_value=(MagicMock(), MagicMock()))
28-
def test_start_measurement_logic(self, mock_subplots, MockBackend):
27+
@patch('matplotlib.figure.Figure.subplots')
28+
def test_start_measurement_logic(self, mock_fig_subplots, MockBackend):
2929
"""
3030
Tests the core logic of the 'Start' button click.
3131
Verifies that parameters are read from the UI and passed to the backend correctly.
3232
"""
3333
# --- Setup ---
34-
# The mock for subplots is now configured directly in the decorator,
35-
# ensuring it's ready before the GUI is instantiated.
34+
# Configure the mock for subplots to return two mock axes
35+
mock_ax_vi = MagicMock()
36+
mock_ax_ri = MagicMock()
37+
mock_fig_subplots.return_value = (mock_ax_vi, mock_ax_ri)
3638
# Instantiate the GUI. This also creates all the tk widgets.
3739
app = MeasurementAppGUI(self.root)
3840

@@ -66,4 +68,5 @@ def test_start_measurement_logic(self, mock_subplots, MockBackend):
6668
self.assertAlmostEqual(passed_params['max_current'], 100e-6)
6769

6870
# 3. Did the GUI generate the correct sweep points?
69-
mock_backend_instance.generate_sweep_points.assert_called_once()
71+
mock_backend_instance.generate_sweep_points.assert_called_once()
72+

tests/test_iv_k2400_loop_backend.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@
99
class TestIVK2400LoopBackend(unittest.TestCase):
1010

1111
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
12-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.pyvisa.ResourceManager')
13-
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
12+
@patch('pymeasure.instruments.keithley.Keithley2400')
1413
@patch('matplotlib.pyplot.show')
1514
@patch('pandas.DataFrame.to_csv')
16-
def test_main_full_run(self, mock_to_csv, mock_plt_show, mock_keithley_class, mock_rm, mock_input):
15+
def test_main_full_run(self, mock_to_csv, mock_plt_show, mock_keithley_class, mock_input):
1716
"""
1817
Test the main function to ensure it runs through the full I-V sweep process.
1918
"""
20-
# --- MOCK SETUP for pyvisa.ResourceManager ---
21-
mock_resource_manager_instance = MagicMock()
22-
mock_rm.return_value = mock_resource_manager_instance
23-
mock_instrument_visa = MagicMock()
24-
mock_resource_manager_instance.open_resource.return_value = mock_instrument_visa
25-
2619
# --- MOCK SETUP for Keithley2400 ---
2720
# Mock the instrument instance
2821
mock_keithley_instance = MagicMock()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock, mock_open, call
3+
4+
# Import the main function from the script we want to test
5+
from Keithley_6517B.High_Resistance.Backends.IV_K6517B_Simple_Backend_v10 import main as iv_simple_main
6+
7+
class TestIVK6517BSimpleBackend(unittest.TestCase):
8+
@patch('time.sleep', MagicMock())
9+
@patch('builtins.input', side_effect=['-10', '10', '5', '0.1', 'test_iv_simple.csv'])
10+
@patch('pymeasure.instruments.keithley.Keithley6517B')
11+
@patch('builtins.open', new_callable=mock_open)
12+
@patch('matplotlib.pyplot.show')
13+
def test_full_run(self, mock_show, mock_file, mock_keithley_class, mock_input):
14+
"""
15+
Tests a complete, successful run of the IV_K6517B_Simple_Backend script.
16+
"""
17+
# --- Setup Mocks for Keithley6517B ---
18+
mock_instrument = MagicMock()
19+
mock_keithley_class.return_value = mock_instrument
20+
# Set a mock ID for the connection message
21+
mock_instrument.id = "Mocked Keithley 6517B"
22+
# Simulate resistance measurement
23+
mock_instrument.resistance = 1.23e9
24+
25+
# --- Run the main function and catch exceptions ---
26+
try:
27+
iv_simple_main()
28+
except Exception as e:
29+
# The script might exit or raise an error after plotting, which is fine for this test
30+
if "no display name" not in str(e): # Ignore matplotlib display errors in CI
31+
pass
32+
33+
# --- Assertions ---
34+
# 1. Was the instrument initialized correctly?
35+
mock_keithley_class.assert_called_once_with(
36+
"GPIB1::27::INSTR", adapter_args={"py_library": "@sim"})
37+
38+
# 2. Was the zero-check and correction sequence performed?
39+
mock_instrument.reset.assert_called_once()
40+
mock_instrument.measure_resistance.assert_called_once()
41+
mock_instrument.write.assert_any_call(':SYSTem:ZCHeck ON')
42+
mock_instrument.write.assert_any_call(':SYSTem:ZCORrect:ACQuire')
43+
mock_instrument.write.assert_any_call(':SYSTem:ZCHeck OFF')
44+
mock_instrument.write.assert_any_call(':SYSTem:ZCORrect ON')
45+
46+
# 3. Was the source configured and enabled?
47+
self.assertEqual(mock_instrument.current_nplc, 1)
48+
mock_instrument.enable_source.assert_called_once()
49+
50+
# 4. Was the voltage sweep performed correctly?
51+
# Based on inputs: start=-10, stop=10, steps=5 -> [-10., -5., 0., 5., 10.]
52+
self.assertEqual(mock_instrument.source_voltage_set.call_count, 5)
53+
mock_instrument.source_voltage_set.assert_any_call(-10.0)
54+
mock_instrument.source_voltage_set.assert_any_call(10.0)
55+
56+
# 5. Was the data file written to?
57+
mock_file.assert_called_with('test_iv_simple.csv', 'w', newline='')
58+
# Header (2) + Data (5)
59+
self.assertTrue(mock_file().write.call_count >= 7)
60+
61+
# 6. Was the instrument shut down safely?
62+
mock_instrument.shutdown.assert_called_once()
63+
64+
# 7. Was the plot shown?
65+
mock_show.assert_called_once()

tests/test_t_control_l350_backend.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,15 @@ def test_get_user_parameters(self, mock_input):
9393
@patch('tkinter.Tk')
9494
@patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv')
9595
@patch('builtins.input', side_effect=['10', '20', '5', '30'])
96-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.pyvisa.ResourceManager')
97-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350') # Correct patch target
98-
@patch('matplotlib.pyplot.subplots')
96+
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350')
9997
@patch('matplotlib.pyplot.show')
10098
@patch('builtins.open', new_callable=mock_open)
10199
@patch('time.sleep', MagicMock())
102100
# Simulate time passing
103101
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
104102
def test_main_runs_and_completes(self, mock_time, mock_open_file,
105-
mock_plt_show, mock_subplots, mock_rm, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
103+
mock_plt_show, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
106104
# --- MOCK SETUP ---
107-
# Configure the mock ResourceManager to return a mock instrument
108-
mock_resource_manager_instance = MagicMock()
109-
mock_rm.return_value = mock_resource_manager_instance
110-
mock_instrument = MagicMock()
111-
mock_resource_manager_instance.open_resource.return_value = mock_instrument
112-
mock_instrument.query.return_value = "Mocked Lakeshore 350 ID" # For IDN query
113-
114105
mock_controller = MagicMock()
115106
mock_ls_class.return_value = mock_controller
116107

@@ -125,7 +116,8 @@ def test_main_runs_and_completes(self, mock_time, mock_open_file,
125116

126117
# --- ASSERTIONS ---
127118
# Check initialization
128-
mock_ls_class.assert_called_once_with("GPIB0::13::INSTR")
119+
mock_ls_class.assert_called_once_with(
120+
"GPIB0::13::INSTR", adapter_args={'py_library': '@sim'})
129121
mock_controller.reset_and_clear.assert_called_once()
130122
mock_controller.setup_heater.assert_called_once()
131123

0 commit comments

Comments
 (0)