|
| 1 | +import unittest |
| 2 | +import sys |
| 3 | +import os |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +# ------------------------------------------------------------------------- |
| 7 | +# GLOBAL MOCKS (The "Headless" GUI Trick) |
| 8 | +# ------------------------------------------------------------------------- |
| 9 | +sys.modules['tkinter'] = MagicMock() |
| 10 | +sys.modules['tkinter.ttk'] = MagicMock() |
| 11 | +sys.modules['tkinter.messagebox'] = MagicMock() |
| 12 | +sys.modules['tkinter.filedialog'] = MagicMock() |
| 13 | +sys.modules['matplotlib'] = MagicMock() |
| 14 | +sys.modules['matplotlib.pyplot'] = MagicMock() |
| 15 | +sys.modules['matplotlib.backends.backend_tkagg'] = MagicMock() |
| 16 | +sys.modules['pyvisa'] = MagicMock() |
| 17 | +sys.modules['pymeasure'] = MagicMock() |
| 18 | +sys.modules['PIL'] = MagicMock() |
| 19 | +sys.modules['PIL.Image'] = MagicMock() |
| 20 | +sys.modules['PIL.ImageTk'] = MagicMock() |
| 21 | + |
| 22 | +class TestFullStack(unittest.TestCase): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.root_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) |
| 26 | + if self.root_dir not in sys.path: |
| 27 | + sys.path.insert(0, self.root_dir) |
| 28 | + |
| 29 | + def test_launcher_buttons(self): |
| 30 | + """ |
| 31 | + Verify that the Launcher buttons point to valid script paths |
| 32 | + and attempt to launch a process. |
| 33 | + """ |
| 34 | + print("\n[FULL-STACK] Testing Launcher Integration...") |
| 35 | + |
| 36 | + try: |
| 37 | + import PICA_v6 as launcher |
| 38 | + except ImportError: |
| 39 | + self.skipTest("Could not import PICA_v6.py") |
| 40 | + |
| 41 | + # We spy on the 'Process' class to see if it gets called |
| 42 | + with patch('multiprocessing.Process') as MockProcess: |
| 43 | + |
| 44 | + # Initialize the App (Headless) |
| 45 | + app = launcher.PICALauncherApp(MagicMock()) |
| 46 | + |
| 47 | + # Pick a button to test (e.g., "K2400 I-V") |
| 48 | + script_key = "K2400 I-V" |
| 49 | + if script_key in app.SCRIPT_PATHS: |
| 50 | + # Simulate the button click action |
| 51 | + target_script = app.SCRIPT_PATHS[script_key] |
| 52 | + app.launch_script(target_script) |
| 53 | + |
| 54 | + # ASSERTION 1: Did we try to spawn a process? |
| 55 | + MockProcess.assert_called() |
| 56 | + |
| 57 | + # ASSERTION 2: Did we pass the correct script? |
| 58 | + _, kwargs = MockProcess.call_args |
| 59 | + args = kwargs.get('args', []) |
| 60 | + if args: |
| 61 | + # The first arg to the process should be the script path |
| 62 | + self.assertIn("IV_K2400_Frontend", str(args[0])) |
| 63 | + print(f" -> Verified: Launcher targeted correct script: {os.path.basename(str(args[0]))}") |
| 64 | + |
| 65 | + # ASSERTION 3: Did we start it? |
| 66 | + MockProcess.return_value.start.assert_called() |
| 67 | + print(" -> Verified: Process launched successfully.") |
| 68 | + else: |
| 69 | + self.fail(f"Key '{script_key}' not found in Launcher config.") |
| 70 | + |
| 71 | +if __name__ == '__main__': |
| 72 | + unittest.main() |
0 commit comments