Skip to content

Commit dc13158

Browse files
hope
1 parent 0697ce7 commit dc13158

6 files changed

Lines changed: 40 additions & 53 deletions

tests/test_fixes.py

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

55
class TestFixes(unittest.TestCase):
66

77
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.sleep', MagicMock())
88
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
9-
@patch('pymeasure.instruments.keithley.Keithley2400') # Patch Keithley2400 from pymeasure
9+
@patch('Keithley_2400.Backends.IV_K2400_Loop_Backend_v10.Keithley2400')
1010
@patch('matplotlib.pyplot.show')
1111
@patch('pandas.DataFrame.to_csv')
12-
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_keithley_class, mock_input): # type: ignore
1313
"""
1414
This test verifies that the Keithley2400 is correctly mocked in the
1515
IV_K2400_Loop_Backend_v10 script, preventing real hardware calls.
@@ -18,26 +18,19 @@ def test_iv_k2400_fix(self, mock_to_csv, mock_plt_show, mock_keithley_class, moc
1818

1919
mock_keithley_instance = MagicMock()
2020
mock_keithley_class.return_value = mock_keithley_instance
21-
mock_keithley_instance.query.return_value = "KEITHLEY INSTRUMENTS INC., MODEL 2400" # Simulate IDN query
21+
mock_keithley_instance.query.return_value = "KEITHLEY INSTRUMENTS INC., MODEL 2400"
2222

2323
iv_backend.main()
2424
mock_keithley_class.assert_called_once_with("GPIB::4")
2525

2626

27-
@patch('tkinter.Tk')
28-
@patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv')
29-
@patch('builtins.input', side_effect=['10', '20', '5', '30'])
30-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.pyvisa.ResourceManager') # Patch pyvisa.ResourceManager
31-
@patch('matplotlib.pyplot.show')
32-
@patch('builtins.open', new_callable=mock_open)
33-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.time.sleep', MagicMock())
34-
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
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):
27+
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350')
28+
def test_t_control_l350_fix(self, mock_ls_class):
3629
from Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10 import main
3730

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")
31+
with patch('builtins.input', side_effect=['10', '20', '5', '30']), \
32+
patch('tkinter.filedialog.asksaveasfilename', return_value='test.csv'), \
33+
patch('matplotlib.pyplot.show'), \
34+
patch('builtins.open', mock_open()):
35+
main()
36+
mock_ls_class.assert_called_once_with("GPIB0::13::INSTR")

tests/test_gui_iv_k2400.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def tearDown(self):
2323
self.root.destroy()
2424

2525
@patch('Keithley_2400.IV_K2400_GUI_v5.Keithley2400_IV_Backend')
26-
@patch('Keithley_2400.IV_K2400_GUI_v5.Figure') # Patch the Figure class itself
27-
def test_start_measurement_logic(self, mock_figure_class, MockBackend):
26+
@patch('matplotlib.figure.Figure.subplots')
27+
def test_start_measurement_logic(self, mock_fig_subplots, MockBackend):
2828
"""
2929
Tests the core logic of the 'Start' button click.
3030
Verifies that parameters are read from the UI and passed to the backend correctly.
@@ -33,7 +33,7 @@ def test_start_measurement_logic(self, mock_figure_class, MockBackend):
3333
# Configure the mock for subplots to return two mock axes
3434
mock_ax_vi = MagicMock()
3535
mock_ax_ri = MagicMock()
36-
mock_figure_class.return_value.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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class TestIVK2400LoopBackend(unittest.TestCase):
1010

1111
@patch('builtins.input', side_effect=['10', '2', 'test_output'])
12-
@patch('pymeasure.instruments.keithley.Keithley2400') # Patch Keithley2400 from pymeasure
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):

tests/test_iv_k6517b_simple_backend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class TestIVK6517BSimpleBackend(unittest.TestCase):
99
@patch('time.sleep', MagicMock())
1010
@patch('builtins.input', side_effect=['-10', '10', '5', '0.1', 'test_iv_simple.csv'])
11-
@patch('pymeasure.instruments.keithley.Keithley6517B') # Patch Keithley6517B from pymeasure
11+
@patch('Keithley_6517B.High_Resistance.Backends.IV_K6517B_Simple_Backend_v10.Keithley6517B')
1212
@patch('builtins.open', new_callable=mock_open)
1313
@patch('matplotlib.pyplot.show')
1414
def test_full_run(self, mock_show, mock_file, mock_keithley_class, mock_input):

tests/test_live_plotter.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import unittest
23
import pandas as pd
34
import matplotlib.pyplot as plt
45
from unittest import mock
@@ -18,19 +19,24 @@ def dummy_csv_file(tmp_path):
1819
df.to_csv(csv_path, index=False)
1920
return csv_path
2021

21-
def test_live_plot_from_csv(dummy_csv_file):
22-
# Mock plt.show() to prevent it from blocking the test
23-
with mock.patch('matplotlib.pyplot.show') as mock_show:
24-
# Mock FuncAnimation to prevent actual animation from running
25-
with mock.patch('matplotlib.animation.FuncAnimation'):
26-
# Mock the plot and scatter methods of the axes
27-
with mock.patch('matplotlib.axes.Axes.plot') as mock_plot, \
28-
mock.patch('matplotlib.axes.Axes.scatter') as mock_scatter:
29-
live_plot_from_csv(dummy_csv_file)
22+
class TestLivePlotter(unittest.TestCase):
23+
@mock.patch('Utilities.LivePlotter_v10.select_file')
24+
@mock.patch('matplotlib.pyplot.show')
25+
@mock.patch('matplotlib.animation.FuncAnimation')
26+
def test_live_plot_from_csv(self, mock_animation, mock_show, mock_select_file, dummy_csv_file):
27+
"""
28+
Tests that the live_plot_from_csv function can be called and attempts to plot.
29+
"""
30+
# Have the mocked select_file return the path to our dummy file
31+
mock_select_file.return_value = str(dummy_csv_file)
3032

31-
# Assert that plt.show() was called
32-
mock_show.assert_called_once()
33+
# We need to run the script's main execution block.
34+
# Since it's under `if __name__ == '__main__':`, we can import it.
35+
# The import itself will trigger the logic.
36+
with mock.patch('Utilities.LivePlotter_v10.live_plot_from_csv') as mock_live_plot:
37+
import Utilities.LivePlotter_v10
38+
# The main block calls live_plot_from_csv, so we check that
39+
mock_live_plot.assert_called_once()
3340

34-
# Assert that plot and scatter were called multiple times
35-
assert mock_plot.call_count >= 3 # For the three subplots
36-
assert mock_scatter.call_count >= 3 # For the three subplots
41+
if __name__ == '__main__':
42+
unittest.main()

tests/test_t_control_l350_backend.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,19 @@ 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.Lakeshore350')
97-
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.plt.subplots') # Patch plt.subplots in the backend module
96+
@patch('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350') # Correct patch target
97+
@patch('matplotlib.pyplot.subplots')
9898
@patch('matplotlib.pyplot.show')
9999
@patch('builtins.open', new_callable=mock_open)
100100
@patch('time.sleep', MagicMock())
101101
# Simulate time passing
102102
@patch('time.time', side_effect=[1000, 1002, 1004, 1006, 1008, 1010])
103103
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):
104+
mock_plt_show, mock_subplots, mock_ls_class, mock_input, mock_file_dialog, mock_tk):
105105
# --- MOCK SETUP ---
106106
mock_controller = MagicMock()
107107
mock_ls_class.return_value = mock_controller
108108

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()
112-
mock_instrument_from_rm = MagicMock()
113-
mock_rm_inside_ls350.open_resource.return_value = mock_instrument_from_rm
114-
mock_instrument_from_rm.query.return_value = "LSCI,MODEL350,12345,1.0" # IDN query
115-
116-
# Configure mock_plt_subplots
117-
mock_fig = MagicMock()
118-
mock_ax = MagicMock()
119-
mock_plt_subplots.return_value = (mock_fig, mock_ax)
120-
121109
# Simulate temperature readings to control the loop
122110
# Start at 10K, then ramp up to 21K to finish
123111
mock_controller.get_temperature.side_effect = [

0 commit comments

Comments
 (0)