94 lines
2.5 KiB
Python
Executable File
94 lines
2.5 KiB
Python
Executable File
from pathlib import Path
|
|
import logging
|
|
|
|
from caproto.sync.client import read
|
|
import pytest
|
|
from pytest import TestReport
|
|
|
|
from src.classes import read_config, IocNotRunning
|
|
|
|
|
|
def check_ioc_running():
|
|
config = read_config()
|
|
pvprefix = config['pvprefix']
|
|
|
|
"""
|
|
This function checks if the test IOC is already running.
|
|
"""
|
|
try:
|
|
read(f'{pvprefix}:IOCREADY', timeout=2)
|
|
|
|
# Reading the check recird was successfull -> We assume that the IOC
|
|
# is running
|
|
return
|
|
except TimeoutError:
|
|
# IOC startup failed in the given time -> Raise an error
|
|
path = Path.cwd() / 'ioc' / 'startioc'
|
|
raise IocNotRunning(f'Start the test IOC first by running {path}')
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def check_ioc_running_before_test():
|
|
check_ioc_running()
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
"""
|
|
This function adds the --stress flag to Pytest which can be given to
|
|
perform long-running stress tests.
|
|
"""
|
|
parser.addoption(
|
|
"--stresstest",
|
|
action="store_true",
|
|
default=False,
|
|
help="Run tests marked as stresstest"
|
|
)
|
|
parser.addoption(
|
|
"--log",
|
|
action="store",
|
|
default="ERROR",
|
|
help="Set log level: DEBUG, INFO, WARNING, ERROR or CRITICAL"
|
|
)
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
|
"""
|
|
This function provides a marker which can be used on test functions to
|
|
mark them as stress tests via `@pytest.mark.stresstest`.
|
|
"""
|
|
if 'stresstest' in item.keywords and not item.config.getoption("--stresstest"):
|
|
pytest.skip("Need --stress to run this test")
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def configure_motor_logging(request):
|
|
"""
|
|
Forward desired loglevel to the motor logger(s).
|
|
"""
|
|
|
|
def parse_loglevel(value: str) -> int:
|
|
value = value.upper()
|
|
if value.isdigit():
|
|
return int(value)
|
|
|
|
if value in logging._nameToLevel:
|
|
return logging._nameToLevel[value]
|
|
|
|
raise ValueError(f"Unknown log level: {value}")
|
|
|
|
loglevel_str = request.config.getoption("--log")
|
|
level = parse_loglevel(loglevel_str)
|
|
logger = logging.getLogger("motor")
|
|
logger.handlers.clear()
|
|
logger.setLevel(level)
|
|
|
|
|
|
def pytest_runtest_logreport(report: TestReport):
|
|
"""
|
|
Print the start of a test if the log level of the motor is INFO or lower.
|
|
"""
|
|
logger = logging.getLogger("motor")
|
|
loglevel = logger.getEffectiveLevel()
|
|
if loglevel <= logging.INFO and report.when == 'call':
|
|
logger.info(f"Running test: {report.nodeid}")
|