Changeset View
Changeset View
Standalone View
Standalone View
tests/atf_python/atf_pytest.py
import types | import types | ||||
from typing import Any | from typing import Any | ||||
from typing import Dict | from typing import Dict | ||||
from typing import List | from typing import List | ||||
from typing import NamedTuple | from typing import NamedTuple | ||||
from typing import Optional | from typing import Optional | ||||
from typing import Tuple | from typing import Tuple | ||||
from atf_python.ktest import generate_ktests | |||||
ngie: Relative imports are better, assuming this is a (namespace )? package. It makes it easier to… | |||||
Done Inline ActionsI don't have a hard opinion on this. Currently, it's a base-only "package" being installed by the FreeBSD makefile. In its current form, the directory structure there somewhat replicates the base system layout, resulting in directory depth being 3-4 items. The imports from the depth-most files may look a bit ugly. melifaro: I don't have a hard opinion on this. Currently, it's a base-only "package" being installed by… | |||||
from atf_python.utils import nodeid_to_method_name | from atf_python.utils import nodeid_to_method_name | ||||
import pytest | import pytest | ||||
import os | import os | ||||
class ATFCleanupItem(pytest.Item): | class ATFCleanupItem(pytest.Item): | ||||
def runtest(self): | def runtest(self): | ||||
Show All 20 Lines | def __init__(self, obj, has_cleanup): | ||||
# Use nodeid without name to properly name class-derived tests | # Use nodeid without name to properly name class-derived tests | ||||
self.ident = obj.nodeid.split("::", 1)[1] | self.ident = obj.nodeid.split("::", 1)[1] | ||||
self.description = self._get_test_description(obj) | self.description = self._get_test_description(obj) | ||||
self.has_cleanup = has_cleanup | self.has_cleanup = has_cleanup | ||||
self.obj = obj | self.obj = obj | ||||
def _get_test_description(self, obj): | def _get_test_description(self, obj): | ||||
"""Returns first non-empty line from func docstring or func name""" | """Returns first non-empty line from func docstring or func name""" | ||||
if getattr(obj, "descr", None) is not None: | |||||
return getattr(obj, "descr") | |||||
docstr = obj.function.__doc__ | docstr = obj.function.__doc__ | ||||
if docstr: | if docstr: | ||||
for line in docstr.split("\n"): | for line in docstr.split("\n"): | ||||
if line: | if line: | ||||
return line | return line | ||||
return obj.name | return obj.name | ||||
@staticmethod | @staticmethod | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | class ATFHandler(object): | ||||
def _generate_test_cleanups(self, items): | def _generate_test_cleanups(self, items): | ||||
new_items = [] | new_items = [] | ||||
for obj in items: | for obj in items: | ||||
if self.has_object_cleanup(obj): | if self.has_object_cleanup(obj): | ||||
self.override_runtest(obj) | self.override_runtest(obj) | ||||
new_items.append(obj) | new_items.append(obj) | ||||
items.clear() | items.clear() | ||||
items.extend(new_items) | items.extend(new_items) | ||||
def expand_tests(self, collector, name, obj): | |||||
return generate_ktests(collector, name, obj) | |||||
def modify_tests(self, items, config): | def modify_tests(self, items, config): | ||||
if config.option.atf_cleanup: | if config.option.atf_cleanup: | ||||
self._generate_test_cleanups(items) | self._generate_test_cleanups(items) | ||||
def list_tests(self, tests: List[str]): | def list_tests(self, tests: List[str]): | ||||
print('Content-Type: application/X-atf-tp; version="1"') | print('Content-Type: application/X-atf-tp; version="1"') | ||||
print() | print() | ||||
▲ Show 20 Lines • Show All 120 Lines • Show Last 20 Lines |
Relative imports are better, assuming this is a (namespace )? package. It makes it easier to lint items like this without installing everything beforehand.