33import pyvisa
44
55
6-
76from Lakeshore_350_340 .Backends .T_Control_L350_Simple_Backend_v10 import (
8- Lakeshore350 , get_user_parameters )
7+ Lakeshore350 , get_user_parameters , main )
98
109
1110class 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
9580class 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