From c5357a4d73f81d537d6bf1a3d3d4e347809e4512 Mon Sep 17 00:00:00 2001 From: Alice Date: Fri, 14 Nov 2025 14:51:09 +0100 Subject: [PATCH] wrapped virtual detector setup in a test fixture --- python/tests/conftest.py | 80 +++++++++++++++++++++++++++++++++ python/tests/test_pythonAPI.py | 6 ++- tests/scripts/test_PythonAPI.py | 70 ----------------------------- 3 files changed, 85 insertions(+), 71 deletions(-) create mode 100644 python/tests/conftest.py delete mode 100644 tests/scripts/test_PythonAPI.py diff --git a/python/tests/conftest.py b/python/tests/conftest.py new file mode 100644 index 000000000..515e86a51 --- /dev/null +++ b/python/tests/conftest.py @@ -0,0 +1,80 @@ +import pytest +import sys +import traceback + +from pathlib import Path + +current_dir = Path(__file__).resolve().parents[2] + +scripts_dir = current_dir / "tests" / "scripts" + +sys.path.append(str(scripts_dir)) + +print(sys.path) + +from utils_for_test import ( + Log, + LogLevel, + RuntimeException, + cleanup, + startProcessInBackground, + startReceiver, + startDetectorVirtualServer, + connectToVirtualServers, + loadConfig, + loadBasicSettings, + runProcessWithLogFile +) + +def pytest_addoption(parser): + parser.addoption( + "--with-detector-simulators", action="store_true", default=False, help="Run tests that require detector simulators" + ) + +def pytest_configure(config): + config.addinivalue_line("markers", "withdetectorsimulators: mark test as needing detector simulators to run") + +def pytest_collection_modifyitems(config, items): + if config.getoption("--with-detector-simulators"): + return + skip = pytest.mark.skip(reason="need --with-detector-simulators option to run") + for item in items: + if "withdetectorsimulators" in item.keywords: + item.add_marker(skip) + +@pytest.fixture +def test_with_simulators(): + """ Fixture to automatically setup virtual detector servers for testing. """ + + LOG_PREFIX_FNAME = '/tmp/slsDetectorPackage_virtual_PythonAPI_test' + MAIN_LOG_FNAME = LOG_PREFIX_FNAME + '_log.txt' + + with open(MAIN_LOG_FNAME, 'w') as fp: + try: + servers = ['moench'] + nmods = 2 + for server in servers: + for ninterfaces in range(1,2): + if ninterfaces == 2 and server != 'jungfrau' and server != 'moench': + continue + + msg = f'Starting Python API Tests for {server}' + + if server == 'jungfrau' or server == 'moench': + msg += f' with {ninterfaces} interfaces' + + Log(LogLevel.INFOBLUE, msg, fp) + cleanup(fp) + startDetectorVirtualServer(server, nmods, fp) + startReceiver(nmods, fp) + d = loadConfig(name=server, log_file_fp=fp, num_mods=nmods, num_frames=1, num_interfaces=ninterfaces) + loadBasicSettings(name=server, d=d, fp=fp) + yield # run test + cleanup(fp) # teardown + except Exception as e: + with open(MAIN_LOG_FNAME, 'a') as fp_error: + traceback.print_exc(file=fp_error) + Log(LogLevel.ERROR, f'Tests Failed.', fp) + cleanup(fp) + + diff --git a/python/tests/test_pythonAPI.py b/python/tests/test_pythonAPI.py index 2e53d42cb..21b5b1d8a 100644 --- a/python/tests/test_pythonAPI.py +++ b/python/tests/test_pythonAPI.py @@ -1,8 +1,12 @@ import pytest +import sys + +from conftest import test_with_simulators from slsdet import Detector -def test_rx_ROI(): +@pytest.mark.withdetectorsimulators +def test_rx_ROI(test_with_simulators): """ Test setting and getting rx_ROI property of Detector class. """ d = Detector() diff --git a/tests/scripts/test_PythonAPI.py b/tests/scripts/test_PythonAPI.py deleted file mode 100644 index 3b4c8ee99..000000000 --- a/tests/scripts/test_PythonAPI.py +++ /dev/null @@ -1,70 +0,0 @@ -# SPDX-License-Identifier: LGPL-3.0-or-other -# Copyright (C) 2021 Contributors to the SLS Detector Package - -''' -This file is used to start up simulators, receivers and test python API, tests are less exhaustive than C++ tests. -''' - -import traceback - -from utils_for_test import ( - Log, - LogLevel, - RuntimeException, - cleanup, - startProcessInBackground, - startReceiver, - startDetectorVirtualServer, - connectToVirtualServers, - loadConfig, - loadBasicSettings, - runProcessWithLogFile -) - -LOG_PREFIX_FNAME = '/tmp/slsDetectorPackage_virtual_PythonAPI_test' -MAIN_LOG_FNAME = LOG_PREFIX_FNAME + '_log.txt' -PYTHONAPI_TEST_FNAME = LOG_PREFIX_FNAME + '_results_' - -def startTests(fp): - servers = [ - 'moench', - ] - nmods = 2 - for server in servers: - for ninterfaces in range(1,2): - if ninterfaces == 2 and server != 'jungfrau' and server != 'moench': - continue - try: - msg = f'Starting Python API Tests for {server}' - if server == 'jungfrau' or server == 'moench': - msg += f' with {ninterfaces} interfaces' - Log(LogLevel.INFOBLUE, msg) - Log(LogLevel.INFOBLUE, msg, fp) - cleanup(fp) - startDetectorVirtualServer(server, nmods, fp) - startReceiver(nmods, fp) - d = loadConfig(name=server, log_file_fp=fp, num_mods=nmods, num_frames=1, num_interfaces=ninterfaces) - loadBasicSettings(name=server, d=d, fp=fp) - - fname = PYTHONAPI_TEST_FNAME + server + '.txt' - cmd = ['python', '-m', 'pytest', '../python/tests/test_pythonAPI.py'] - runProcessWithLogFile('Python API Tests for ' + server, cmd, fp, fname) - Log(LogLevel.INFO, '\n') - except Exception as e: - raise RuntimeException(f'Python API Tests failed') from e - - Log(LogLevel.INFOGREEN, 'Passed all Python API tests for server ' + str(servers) + '\n') - - -if __name__ == '__main__': - Log(LogLevel.INFOBLUE, '\nLog File: ' + MAIN_LOG_FNAME + '\n') - - with open(MAIN_LOG_FNAME, 'w') as fp: - try: - startTests(fp) - cleanup(fp) - except Exception as e: - with open(MAIN_LOG_FNAME, 'a') as fp_error: - traceback.print_exc(file=fp_error) - cleanup(fp) - Log(LogLevel.ERROR, f'Tests Failed.') \ No newline at end of file