mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
ci(pytest): move to class plugin
This commit is contained in:
parent
db8fb1f47b
commit
f64d25191e
151
conftest.py
151
conftest.py
@ -147,74 +147,95 @@ def pytest_addoption(parser: pytest.Parser) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(tryfirst=True)
|
_idf_pytest_embedded_key = pytest.StashKey['IdfPytestEmbedded']
|
||||||
def pytest_sessionstart(session: Session) -> None:
|
|
||||||
if session.config.option.target:
|
|
||||||
session.config.option.target = session.config.getoption('target').lower()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(tryfirst=True)
|
def pytest_configure(config: Config) -> None:
|
||||||
def pytest_collection_modifyitems(config: Config, items: List[Function]) -> None:
|
config.stash[_idf_pytest_embedded_key] = IdfPytestEmbedded(
|
||||||
target = config.getoption('target', None) # use the `build` dir
|
target=config.getoption('target'),
|
||||||
if not target:
|
sdkconfig=config.getoption('sdkconfig'),
|
||||||
return
|
)
|
||||||
|
config.pluginmanager.register(config.stash[_idf_pytest_embedded_key])
|
||||||
# sort by file path and callspec.config
|
|
||||||
# implement like this since this is a limitation of pytest, couldn't get fixture values while collecting
|
|
||||||
# https://github.com/pytest-dev/pytest/discussions/9689
|
|
||||||
def _get_param_config(_item: Function) -> str:
|
|
||||||
if hasattr(_item, 'callspec'):
|
|
||||||
return _item.callspec.params.get('config', DEFAULT_SDKCONFIG) # type: ignore
|
|
||||||
return DEFAULT_SDKCONFIG
|
|
||||||
|
|
||||||
items.sort(key=lambda x: (os.path.dirname(x.path), _get_param_config(x)))
|
|
||||||
|
|
||||||
# add markers for special markers
|
|
||||||
for item in items:
|
|
||||||
if 'supported_targets' in item_marker_names(item):
|
|
||||||
for _target in SUPPORTED_TARGETS:
|
|
||||||
item.add_marker(_target)
|
|
||||||
if 'preview_targets' in item_marker_names(item):
|
|
||||||
for _target in PREVIEW_TARGETS:
|
|
||||||
item.add_marker(_target)
|
|
||||||
if 'all_targets' in item_marker_names(item):
|
|
||||||
for _target in [*SUPPORTED_TARGETS, *PREVIEW_TARGETS]:
|
|
||||||
item.add_marker(_target)
|
|
||||||
|
|
||||||
# filter all the test cases with "--target"
|
|
||||||
items[:] = [item for item in items if target in item_marker_names(item)]
|
|
||||||
|
|
||||||
# filter all the test cases with cli option "config"
|
|
||||||
if config.getoption('sdkconfig'):
|
|
||||||
items[:] = [
|
|
||||||
item
|
|
||||||
for item in items
|
|
||||||
if _get_param_config(item) == config.getoption('sdkconfig')
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(trylast=True)
|
def pytest_unconfigure(config: Config) -> None:
|
||||||
def pytest_runtest_teardown(item: Function) -> None:
|
_pytest_embedded = config.stash.get(_idf_pytest_embedded_key, None)
|
||||||
"""
|
if _pytest_embedded:
|
||||||
Format the test case generated junit reports
|
del config.stash[_idf_pytest_embedded_key]
|
||||||
"""
|
config.pluginmanager.unregister(_pytest_embedded)
|
||||||
tempdir = item.funcargs.get('test_case_tempdir')
|
|
||||||
if not tempdir:
|
|
||||||
return
|
|
||||||
|
|
||||||
junits = find_by_suffix('.xml', tempdir)
|
|
||||||
if not junits:
|
|
||||||
return
|
|
||||||
|
|
||||||
target = item.funcargs['target']
|
class IdfPytestEmbedded:
|
||||||
config = item.funcargs['config']
|
|
||||||
for junit in junits:
|
def __init__(self, target: Optional[str] = None, sdkconfig: Optional[str] = None):
|
||||||
xml = ET.parse(junit)
|
# CLI options to filter the test cases
|
||||||
testcases = xml.findall('.//testcase')
|
self.target = target
|
||||||
for case in testcases:
|
self.sdkconfig = sdkconfig
|
||||||
case.attrib['name'] = format_case_id(target, config, case.attrib['name'])
|
|
||||||
if 'file' in case.attrib:
|
@pytest.hookimpl(tryfirst=True)
|
||||||
case.attrib['file'] = case.attrib['file'].replace(
|
def pytest_sessionstart(self, session: Session) -> None:
|
||||||
'/IDF/', ''
|
if self.target:
|
||||||
) # our unity test framework
|
self.target = self.target.lower()
|
||||||
xml.write(junit)
|
session.config.option.target = self.target
|
||||||
|
|
||||||
|
@pytest.hookimpl(tryfirst=True)
|
||||||
|
def pytest_collection_modifyitems(self, items: List[Function]) -> None:
|
||||||
|
# sort by file path and callspec.config
|
||||||
|
# implement like this since this is a limitation of pytest, couldn't get fixture values while collecting
|
||||||
|
# https://github.com/pytest-dev/pytest/discussions/9689
|
||||||
|
def _get_param_config(_item: Function) -> str:
|
||||||
|
if hasattr(_item, 'callspec'):
|
||||||
|
return _item.callspec.params.get('config', DEFAULT_SDKCONFIG) # type: ignore
|
||||||
|
return DEFAULT_SDKCONFIG
|
||||||
|
|
||||||
|
items.sort(key=lambda x: (os.path.dirname(x.path), _get_param_config(x)))
|
||||||
|
|
||||||
|
# add markers for special markers
|
||||||
|
for item in items:
|
||||||
|
if 'supported_targets' in item_marker_names(item):
|
||||||
|
for _target in SUPPORTED_TARGETS:
|
||||||
|
item.add_marker(_target)
|
||||||
|
if 'preview_targets' in item_marker_names(item):
|
||||||
|
for _target in PREVIEW_TARGETS:
|
||||||
|
item.add_marker(_target)
|
||||||
|
if 'all_targets' in item_marker_names(item):
|
||||||
|
for _target in [*SUPPORTED_TARGETS, *PREVIEW_TARGETS]:
|
||||||
|
item.add_marker(_target)
|
||||||
|
|
||||||
|
# filter all the test cases with "--target"
|
||||||
|
if self.target:
|
||||||
|
items[:] = [item for item in items if self.target in item_marker_names(item)]
|
||||||
|
|
||||||
|
# filter all the test cases with cli option "config"
|
||||||
|
if self.sdkconfig:
|
||||||
|
items[:] = [
|
||||||
|
item
|
||||||
|
for item in items
|
||||||
|
if _get_param_config(item) == self.sdkconfig
|
||||||
|
]
|
||||||
|
|
||||||
|
@pytest.hookimpl(trylast=True)
|
||||||
|
def pytest_runtest_teardown(self, item: Function) -> None:
|
||||||
|
"""
|
||||||
|
Format the test case generated junit reports
|
||||||
|
"""
|
||||||
|
tempdir = item.funcargs.get('test_case_tempdir')
|
||||||
|
if not tempdir:
|
||||||
|
return
|
||||||
|
|
||||||
|
junits = find_by_suffix('.xml', tempdir)
|
||||||
|
if not junits:
|
||||||
|
return
|
||||||
|
|
||||||
|
target = item.funcargs['target']
|
||||||
|
config = item.funcargs['config']
|
||||||
|
for junit in junits:
|
||||||
|
xml = ET.parse(junit)
|
||||||
|
testcases = xml.findall('.//testcase')
|
||||||
|
for case in testcases:
|
||||||
|
case.attrib['name'] = format_case_id(target, config, case.attrib['name'])
|
||||||
|
if 'file' in case.attrib:
|
||||||
|
case.attrib['file'] = case.attrib['file'].replace(
|
||||||
|
'/IDF/', ''
|
||||||
|
) # our unity test framework
|
||||||
|
xml.write(junit)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user