2023-08-04 18:27:03 +00:00
|
|
|
import logging
|
|
|
|
from datetime import datetime
|
|
|
|
|
2023-12-05 22:31:44 +00:00
|
|
|
import os
|
2023-08-04 18:27:03 +00:00
|
|
|
import allure
|
2023-07-10 15:55:10 +00:00
|
|
|
import pytest
|
2024-07-08 10:26:05 +00:00
|
|
|
import shortuuid
|
2023-08-04 18:27:03 +00:00
|
|
|
from PIL import ImageGrab
|
|
|
|
|
|
|
|
import configs
|
2024-06-18 06:53:37 +00:00
|
|
|
from configs.system import get_platform
|
2023-10-09 17:04:29 +00:00
|
|
|
from fixtures.path import generate_test_info
|
2023-08-04 18:27:03 +00:00
|
|
|
from scripts.utils.system_path import SystemPath
|
|
|
|
|
2023-11-30 13:07:26 +00:00
|
|
|
# Send logs to pytest.log as well
|
|
|
|
handler = logging.FileHandler(filename=configs.PYTEST_LOG)
|
|
|
|
logging.basicConfig(
|
2023-12-05 22:31:44 +00:00
|
|
|
level=os.getenv('LOG_LEVEL', 'INFO'),
|
2023-12-01 16:40:42 +00:00
|
|
|
format='[%(asctime)s] (%(filename)18s:%(lineno)-3s) [%(levelname)-7s] --- %(message)s',
|
2023-11-30 13:07:26 +00:00
|
|
|
handlers=[handler],
|
|
|
|
)
|
|
|
|
LOG = logging.getLogger(__name__)
|
2023-07-10 15:55:10 +00:00
|
|
|
|
|
|
|
pytest_plugins = [
|
2023-09-28 12:44:13 +00:00
|
|
|
'fixtures.aut',
|
|
|
|
'fixtures.path',
|
|
|
|
'fixtures.squish',
|
|
|
|
'fixtures.testrail',
|
2023-07-10 15:55:10 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session', autouse=True)
|
|
|
|
def setup_session_scope(
|
2023-08-04 18:27:03 +00:00
|
|
|
init_testrail_api,
|
|
|
|
prepare_test_directory,
|
|
|
|
start_squish_server,
|
|
|
|
):
|
2023-11-30 13:07:26 +00:00
|
|
|
LOG.info('Session startup...')
|
2023-08-04 18:27:03 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def setup_function_scope(
|
2023-08-10 11:43:17 +00:00
|
|
|
caplog,
|
2023-08-04 18:27:03 +00:00
|
|
|
generate_test_data,
|
2024-03-12 09:17:20 +00:00
|
|
|
check_result,
|
2024-07-12 09:44:29 +00:00
|
|
|
application_logs,
|
|
|
|
keycard_controller
|
2023-07-10 15:55:10 +00:00
|
|
|
):
|
2023-11-30 13:07:26 +00:00
|
|
|
# FIXME: broken due to KeyError: <_pytest.stash.StashKey object at 0x7fd1ba6d78c0>
|
2024-03-06 15:05:02 +00:00
|
|
|
# caplog.set_level(configs.LOG_LEVEL)
|
2023-07-10 15:55:10 +00:00
|
|
|
yield
|
|
|
|
|
|
|
|
|
2023-08-04 18:27:03 +00:00
|
|
|
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
|
|
outcome = yield
|
|
|
|
rep = outcome.get_result()
|
|
|
|
setattr(item, 'rep_' + rep.when, rep)
|
|
|
|
|
|
|
|
|
2023-07-10 15:55:10 +00:00
|
|
|
def pytest_exception_interact(node):
|
2023-11-30 13:07:26 +00:00
|
|
|
test_path, test_name, test_params = generate_test_info(node)
|
|
|
|
node_dir: SystemPath = configs.testpath.RUN / test_path / test_name / test_params
|
|
|
|
node_dir.mkdir(parents=True, exist_ok=True)
|
2024-07-08 10:26:05 +00:00
|
|
|
screenshot = node_dir / f'screenshot_{shortuuid.ShortUUID().random(length=10)}.png'
|
|
|
|
try:
|
|
|
|
ImageGrab.grab(xdisplay=configs.system.DISPLAY if get_platform() == "Linux" else None).save(screenshot)
|
|
|
|
allure.attach(
|
|
|
|
name='Screenshot on fail',
|
|
|
|
body=screenshot.read_bytes(),
|
|
|
|
attachment_type=allure.attachment_type.PNG
|
|
|
|
)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print("Screenshot was not generated or saved")
|