tests: refactor atf_python a bit

* Move more logic from conftest.py to the actual atf_pytest handler
* Move nodeid_to_method_name() to the utils.py so it can be shared

MFC after:	2 weeks
This commit is contained in:
Alexander V. Chernikov 2023-04-01 19:40:54 +00:00
parent fc2538cb7b
commit d9af4219d6
3 changed files with 22 additions and 14 deletions

View File

@ -6,15 +6,12 @@ from typing import NamedTuple
from typing import Optional
from typing import Tuple
from atf_python.utils import nodeid_to_method_name
import pytest
import os
def nodeid_to_method_name(nodeid: str) -> str:
"""file_name.py::ClassName::method_name[parametrize] -> method_name"""
return nodeid.split("::")[-1].split("[")[0]
class ATFCleanupItem(pytest.Item):
def runtest(self):
"""Runs cleanup procedure for the test instead of the test itself"""
@ -73,7 +70,6 @@ class ATFTestObj(object):
else:
ret["require.user"] = username
def _convert_marks(self, obj) -> Dict[str, Any]:
wj_func = lambda x: " ".join(x) # noqa: E731
_map: Dict[str, Dict] = {
@ -158,6 +154,19 @@ class ATFHandler(object):
return True
return False
def _generate_test_cleanups(self, items):
new_items = []
for obj in items:
if self.has_object_cleanup(obj):
self.override_runtest(obj)
new_items.append(obj)
items.clear()
items.extend(new_items)
def modify_tests(self, items, config):
if config.option.atf_cleanup:
self._generate_test_cleanups(items)
def list_tests(self, tests: List[str]):
print('Content-Type: application/X-atf-tp; version="1"')
print()

View File

@ -11,6 +11,11 @@ from typing import Optional
import pytest
def nodeid_to_method_name(nodeid: str) -> str:
"""file_name.py::ClassName::method_name[parametrize] -> method_name"""
return nodeid.split("::")[-1].split("[")[0]
class LibCWrapper(object):
def __init__(self):
path: Optional[str] = find_library("c")

View File

@ -101,15 +101,9 @@ def pytest_configure(config):
def pytest_collection_modifyitems(session, config, items):
"""If cleanup is requested, replace collected tests with their cleanups (if any)"""
if PLUGIN_ENABLED and config.option.atf_cleanup:
new_items = []
if PLUGIN_ENABLED:
handler = get_handler()
for obj in items:
if handler.has_object_cleanup(obj):
handler.override_runtest(obj)
new_items.append(obj)
items.clear()
items.extend(new_items)
handler.modify_tests(items, config)
def pytest_collection_finish(session):