2022-11-14 00:09:19 +01:00
|
|
|
import unittest
|
|
|
|
|
from tests.libreVNA import libreVNA as libreVNA
|
|
|
|
|
import tests.definitions as defs
|
|
|
|
|
import subprocess
|
|
|
|
|
import time
|
|
|
|
|
import select
|
2025-01-06 17:34:46 +01:00
|
|
|
import os
|
2022-11-14 00:09:19 +01:00
|
|
|
from signal import SIGINT
|
|
|
|
|
|
|
|
|
|
class TestBase(unittest.TestCase):
|
|
|
|
|
def setUp(self):
|
2025-01-06 17:34:46 +01:00
|
|
|
f = open("log.txt", "w")
|
|
|
|
|
self.gui = subprocess.Popen([defs.GUI_PATH, '-p', '19544', '--reset-preferences', '--no-gui', '-platform', 'offscreen'], stdout=f, stderr=subprocess.STDOUT)
|
2022-11-14 00:09:19 +01:00
|
|
|
|
|
|
|
|
# wait for the SCPI server to become available
|
|
|
|
|
timeout = time.time() + 3;
|
2025-01-06 17:34:46 +01:00
|
|
|
fread = open("log.txt", "r")
|
2024-12-17 12:06:55 +01:00
|
|
|
while True:
|
2025-01-06 17:34:46 +01:00
|
|
|
text = fread.read()
|
|
|
|
|
if text and "Listening on port 19544" in text:
|
|
|
|
|
break
|
2024-12-17 12:06:55 +01:00
|
|
|
if time.time() >= timeout:
|
|
|
|
|
self.tearDown()
|
|
|
|
|
raise AssertionError("Timed out waiting for SCPI server")
|
|
|
|
|
|
2024-12-02 12:10:13 +01:00
|
|
|
self.vna = libreVNA('localhost', 19544, timeout=4)
|
2022-11-19 15:47:08 +01:00
|
|
|
try:
|
2025-01-06 17:34:46 +01:00
|
|
|
self.vna.cmd("*CLS")
|
2022-11-19 15:47:08 +01:00
|
|
|
except Exception as e:
|
|
|
|
|
self.tearDown()
|
|
|
|
|
raise e
|
2022-11-14 00:09:19 +01:00
|
|
|
if self.vna.query(":DEV:CONN?") == "Not connected":
|
|
|
|
|
self.tearDown()
|
|
|
|
|
raise AssertionError("Not connected")
|
2025-01-06 17:34:46 +01:00
|
|
|
|
|
|
|
|
|
2022-11-14 00:09:19 +01:00
|
|
|
def tearDown(self):
|
2025-01-06 17:34:46 +01:00
|
|
|
if hasattr(self._outcome, 'errors'):
|
|
|
|
|
# Python 3.4 - 3.10 (These two methods have no side effects)
|
|
|
|
|
result = self.defaultTestResult()
|
|
|
|
|
self._feedErrorsToResult(result, self._outcome.errors)
|
|
|
|
|
else:
|
|
|
|
|
# Python 3.11+
|
|
|
|
|
result = self._outcome.result
|
|
|
|
|
ok = all(test != self for test, text in result.errors + result.failures)
|
|
|
|
|
|
|
|
|
|
if not ok:
|
|
|
|
|
try:
|
|
|
|
|
# Try to grab the packet log and save it
|
|
|
|
|
packetlog = self.vna.query(":DEV:PACKETLOG?", timeout=5)
|
|
|
|
|
f = open("packetlog_"+time.strftime("%Y%m%d%H%M%S.vnalog"), "w")
|
|
|
|
|
f.write(packetlog)
|
|
|
|
|
f.close()
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
|
2024-12-17 12:06:55 +01:00
|
|
|
# make sure the GUI did not crash during the test
|
2025-01-06 17:34:46 +01:00
|
|
|
crashed = self.gui.poll() is not None
|
2022-11-14 00:09:19 +01:00
|
|
|
self.gui.send_signal(SIGINT)
|
|
|
|
|
try:
|
2024-05-01 15:37:39 +02:00
|
|
|
self.gui.wait(timeout = 3)
|
2022-11-14 00:09:19 +01:00
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
|
self.gui.kill()
|
|
|
|
|
|
2025-01-06 17:34:46 +01:00
|
|
|
|
|
|
|
|
if ok and not crashed:
|
|
|
|
|
# remove log file
|
|
|
|
|
os.remove("log.txt")
|
|
|
|
|
else:
|
|
|
|
|
os.rename("log.txt", "errorlog_"+time.strftime("%Y%m%d%H%M%S.txt"))
|
|
|
|
|
|
|
|
|
|
if(crashed):
|
|
|
|
|
raise Exception("GUI crashed")
|
|
|
|
|
|
2023-10-24 16:39:21 +02:00
|
|
|
|