LibreVNA/Software/Integrationtests/tests/TestBase.py

76 lines
2.4 KiB
Python
Raw Normal View History

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
import os
2022-11-14 00:09:19 +01:00
from signal import SIGINT
class TestBase(unittest.TestCase):
def setUp(self):
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;
fread = open("log.txt", "r")
2024-12-17 12:06:55 +01:00
while True:
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:
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")
2022-11-14 00:09:19 +01:00
def tearDown(self):
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
crashed = self.gui.poll() is not None
2022-11-14 00:09:19 +01:00
self.gui.send_signal(SIGINT)
try:
self.gui.wait(timeout = 3)
2022-11-14 00:09:19 +01:00
except subprocess.TimeoutExpired:
self.gui.kill()
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")