Changeset View
Changeset View
Standalone View
Standalone View
tests/conftest.py
import pytest | import pytest | ||||
from atf_python.atf_pytest import ATFHandler | from atf_python.atf_pytest import ATFHandler | ||||
from typing import Dict | from typing import Dict | ||||
PLUGIN_ENABLED = False | PLUGIN_ENABLED = False | ||||
DEFAULT_HANDLER = None | DEFAULT_HANDLER = None | ||||
def set_handler(config): | |||||
global DEFAULT_HANDLER, PLUGIN_ENABLED | |||||
DEFAULT_HANDLER = ATFHandler(report_file_name=config.option.atf_file) | |||||
PLUGIN_ENABLED = True | |||||
return DEFAULT_HANDLER | |||||
def get_handler(): | def get_handler(): | ||||
global DEFAULT_HANDLER | |||||
if DEFAULT_HANDLER is None: | |||||
DEFAULT_HANDLER = ATFHandler() | |||||
return DEFAULT_HANDLER | return DEFAULT_HANDLER | ||||
def pytest_addoption(parser): | def pytest_addoption(parser): | ||||
"""Add file output""" | """Add file output""" | ||||
# Add meta-values | # Add meta-values | ||||
group = parser.getgroup("general", "Running and selection options") | group = parser.getgroup("general", "Running and selection options") | ||||
group.addoption( | group.addoption( | ||||
Show All 30 Lines | |||||
@pytest.hookimpl(trylast=True) | @pytest.hookimpl(trylast=True) | ||||
def pytest_configure(config): | def pytest_configure(config): | ||||
if config.option.help: | if config.option.help: | ||||
return | return | ||||
# Register markings anyway to avoid warnings | # Register markings anyway to avoid warnings | ||||
config.addinivalue_line("markers", "require_user(name): user to run the test with") | config.addinivalue_line("markers", "require_user(name): user to run the test with") | ||||
Lint: PEP8 E501: line too long (87 > 79 characters) | |||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_arch(names): List[str] of support archs" | "markers", "require_arch(names): List[str] of support archs" | ||||
) | ) | ||||
# config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs") | # config.addinivalue_line("markers", "require_config(config): List[Tuple[str,Any]] of k=v pairs") | ||||
Lint: PEP8 E501 line too long (101 > 79 characters) Lint: PEP8 E501: line too long (101 > 79 characters) | |||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_diskspace(amount): str with required diskspace" | "markers", "require_diskspace(amount): str with required diskspace" | ||||
) | ) | ||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_files(space): List[str] with file paths" | "markers", "require_files(space): List[str] with file paths" | ||||
) | ) | ||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_machine(names): List[str] of support machine types" | "markers", "require_machine(names): List[str] of support machine types" | ||||
) | ) | ||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_memory(amount): str with required memory" | "markers", "require_memory(amount): str with required memory" | ||||
) | ) | ||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "require_progs(space): List[str] with file paths" | "markers", "require_progs(space): List[str] with file paths" | ||||
) | ) | ||||
config.addinivalue_line( | config.addinivalue_line( | ||||
"markers", "timeout(dur): int/float with max duration in sec" | "markers", "timeout(dur): int/float with max duration in sec" | ||||
) | ) | ||||
global PLUGIN_ENABLED | if not config.option.atf: | ||||
PLUGIN_ENABLED = config.option.atf | |||||
if not PLUGIN_ENABLED: | |||||
return | return | ||||
get_handler() | handler = set_handler(config) | ||||
if config.option.collectonly: | if config.option.collectonly: | ||||
# Need to output list of tests to stdout, hence override | # Need to output list of tests to stdout, hence override | ||||
# standard reporter plugin | # standard reporter plugin | ||||
reporter = config.pluginmanager.getplugin("terminalreporter") | reporter = config.pluginmanager.getplugin("terminalreporter") | ||||
if reporter: | if reporter: | ||||
config.pluginmanager.unregister(reporter) | config.pluginmanager.unregister(reporter) | ||||
else: | |||||
handler.setup_configure() | |||||
def pytest_collection_modifyitems(session, config, items): | def pytest_collection_modifyitems(session, config, items): | ||||
"""If cleanup is requested, replace collected tests with their cleanups (if any)""" | """If cleanup is requested, replace collected tests with their cleanups (if any)""" | ||||
Lint: PEP8 E501 line too long (87 > 79 characters) Lint: PEP8 E501: line too long (87 > 79 characters) | |||||
if PLUGIN_ENABLED and config.option.atf_cleanup: | if PLUGIN_ENABLED and config.option.atf_cleanup: | ||||
new_items = [] | new_items = [] | ||||
handler = get_handler() | handler = get_handler() | ||||
for obj in items: | for obj in items: | ||||
if handler.has_object_cleanup(obj): | if handler.has_object_cleanup(obj): | ||||
handler.override_runtest(obj) | handler.override_runtest(obj) | ||||
new_items.append(obj) | new_items.append(obj) | ||||
items.clear() | items.clear() | ||||
items.extend(new_items) | items.extend(new_items) | ||||
def pytest_collection_finish(session): | def pytest_collection_finish(session): | ||||
if PLUGIN_ENABLED and session.config.option.collectonly: | if PLUGIN_ENABLED and session.config.option.collectonly: | ||||
handler = get_handler() | handler = get_handler() | ||||
handler.list_tests(session.items) | handler.list_tests(session.items) | ||||
def pytest_runtest_setup(item): | |||||
if PLUGIN_ENABLED: | |||||
handler = get_handler() | |||||
handler.setup_method_pre(item) | |||||
def pytest_runtest_logreport(report): | def pytest_runtest_logreport(report): | ||||
if PLUGIN_ENABLED: | if PLUGIN_ENABLED: | ||||
handler = get_handler() | handler = get_handler() | ||||
handler.add_report(report) | handler.add_report(report) | ||||
def pytest_unconfigure(config): | def pytest_unconfigure(config): | ||||
if PLUGIN_ENABLED and config.option.atf_file: | if PLUGIN_ENABLED and config.option.atf_file: | ||||
handler = get_handler() | handler = get_handler() | ||||
handler.write_report(config.option.atf_file) | handler.write_report() |
line too long (87 > 79 characters)