11import unittest
22from unittest .mock import patch , MagicMock , mock_open
33import pyvisa
4- import time
54import sys
65import os
76
@@ -31,7 +30,7 @@ def test_initialization_success(self):
3130 @patch ('pyvisa.ResourceManager' )
3231 def test_initialization_failure (self , mock_rm ):
3332 # Test that a connection error is raised if pyvisa fails
34- mock_rm .return_value .open_resource .side_effect = pyvisa .errors .VisaIOError ("Test Error" )
33+ mock_rm .return_value .open_resource .side_effect = pyvisa .errors .VisaIOError (pyvisa . constants . VI_ERROR_RSRC_NFOUND )
3534 with self .assertRaises (ConnectionError ):
3635 Lakeshore350 ("GPIB0::13::INSTR" )
3736
@@ -79,7 +78,7 @@ def test_close(self):
7978 self .assertIsNone (self .controller .instrument )
8079
8180class TestMainFunctionAndUserInput (unittest .TestCase ):
82- @patch ('builtins.input' , side_effect = ['100' , '200' , '300' , '10' , 'not-a-number' , '50' , '350' , '400' ])
81+ @patch ('builtins.input' , side_effect = ['100' , '200' , '300' , '10' , 'not-a-number' , '50' , '350' , '400' , '10' ])
8382 def test_get_user_parameters (self , mock_input ):
8483 # First call: Valid input
8584 start , end , rate , cutoff = get_user_parameters ()
@@ -89,15 +88,17 @@ def test_get_user_parameters(self, mock_input):
8988 # Second call: Invalid text input, should retry and get the next valid ones
9089 start , end , rate , cutoff = get_user_parameters ()
9190 self .assertEqual ((start , end , cutoff ), (50 , 350 , 400 ))
92-
91+ self .assertEqual (rate , 10 )
92+
93+ @patch ('tkinter.Tk' )
9394 @patch ('tkinter.filedialog.asksaveasfilename' , return_value = 'test.csv' )
9495 @patch ('builtins.input' , side_effect = ['10' , '20' , '5' , '30' ])
9596 @patch ('Lakeshore_350_340.Backends.T_Control_L350_Simple_Backend_v10.Lakeshore350' )
9697 @patch ('matplotlib.pyplot.show' )
9798 @patch ('builtins.open' , new_callable = mock_open )
9899 @patch ('time.sleep' , MagicMock ())
99100 @patch ('time.time' , side_effect = [1000 , 1002 , 1004 , 1006 , 1008 , 1010 ]) # Simulate time passing
100- def test_main_runs_and_completes (self , mock_time , mock_open_file , mock_plt_show , mock_ls_class , mock_input , mock_file_dialog ):
101+ def test_main_runs_and_completes (self , mock_time , mock_open_file , mock_plt_show , mock_ls_class , mock_input , mock_file_dialog , mock_tk ):
101102 # --- MOCK SETUP ---
102103 mock_controller = MagicMock ()
103104 mock_ls_class .return_value = mock_controller
0 commit comments