11import unittest
22from unittest .mock import patch , MagicMock
3- import sys
4- import os
53
6- # Add the root directory to the Python path
7- sys .path .insert (0 , os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
84
95# Now we can import the module to be tested
106from Keithley_2400 .Backends import IV_K2400_Loop_Backend_v10 as iv_backend
117
8+
129class TestIVK2400LoopBackend (unittest .TestCase ):
1310
1411 @patch ('builtins.input' , side_effect = ['10' , '2' , 'test_output' ])
@@ -28,20 +25,22 @@ def test_main_full_run(self, mock_to_csv, mock_plt_show, mock_keithley_class, mo
2825 # Let's return a voltage that is proportional to the current
2926 def voltage_side_effect ():
3027 # A simple linear relationship for testing
31- return mock_keithley_instance .source_current * 10
28+ return mock_keithley_instance .source_current * 10
3229
3330 # We can't directly access the source_current from ramp_to_current,
3431 # so we will use a side effect to track it
3532 latest_current = [0 ]
33+
3634 def ramp_side_effect (current ):
3735 latest_current [0 ] = current
3836 # Also update the mock's internal state
3937 mock_keithley_instance .source_current = current
40-
38+
4139 mock_keithley_instance .ramp_to_current .side_effect = ramp_side_effect
42-
40+
4341 # When .voltage is accessed, return the calculated value
44- type(mock_keithley_instance ).voltage = unittest .mock .PropertyMock (side_effect = voltage_side_effect )
42+ type(mock_keithley_instance ).voltage = unittest .mock .PropertyMock (
43+ side_effect = voltage_side_effect )
4544
4645 # --- EXECUTE SCRIPT ---
4746 iv_backend .main ()
@@ -63,9 +62,11 @@ def ramp_side_effect(current):
6362 # np.linspace(0, 10, int(10/2) + 1) -> linspace(0, 10, 6)
6463 # The values will be [0., 2., 4., 6., 8., 10.]
6564 expected_currents_uA = [0. , 2. , 4. , 6. , 8. , 10. ]
66- expected_calls = [unittest .mock .call (c * 1e-6 ) for c in expected_currents_uA ]
67- mock_keithley_instance .ramp_to_current .assert_has_calls (expected_calls )
68-
65+ expected_calls = [unittest .mock .call (
66+ c * 1e-6 ) for c in expected_currents_uA ]
67+ mock_keithley_instance .ramp_to_current .assert_has_calls (
68+ expected_calls )
69+
6970 # 4. Verify the number of measurements taken
7071 self .assertEqual (mock_keithley_instance .ramp_to_current .call_count , 6 )
7172 # Voltage is read once per loop
@@ -77,12 +78,13 @@ def ramp_side_effect(current):
7778 call_args = mock_to_csv .call_args
7879 save_path = call_args [0 ][0 ]
7980 self .assertIn ('test_output.txt' , save_path )
80-
81+
8182 # 6. Check that the instrument was shut down
8283 mock_keithley_instance .shutdown .assert_called_once ()
83-
84+
8485 # 7. Check that the plot was displayed
8586 mock_plt_show .assert_called_once ()
8687
88+
8789if __name__ == '__main__' :
8890 unittest .main ()
0 commit comments