ci(pytest): move to class plugin

This commit is contained in:
Fu Hanxi 2022-03-14 11:53:36 +08:00
parent db8fb1f47b
commit f64d25191e

View File

@ -147,18 +147,39 @@ def pytest_addoption(parser: pytest.Parser) -> None:
)
@pytest.hookimpl(tryfirst=True)
def pytest_sessionstart(session: Session) -> None:
if session.config.option.target:
session.config.option.target = session.config.getoption('target').lower()
_idf_pytest_embedded_key = pytest.StashKey['IdfPytestEmbedded']
@pytest.hookimpl(tryfirst=True)
def pytest_collection_modifyitems(config: Config, items: List[Function]) -> None:
target = config.getoption('target', None) # use the `build` dir
if not target:
return
def pytest_configure(config: Config) -> None:
config.stash[_idf_pytest_embedded_key] = IdfPytestEmbedded(
target=config.getoption('target'),
sdkconfig=config.getoption('sdkconfig'),
)
config.pluginmanager.register(config.stash[_idf_pytest_embedded_key])
def pytest_unconfigure(config: Config) -> None:
_pytest_embedded = config.stash.get(_idf_pytest_embedded_key, None)
if _pytest_embedded:
del config.stash[_idf_pytest_embedded_key]
config.pluginmanager.unregister(_pytest_embedded)
class IdfPytestEmbedded:
def __init__(self, target: Optional[str] = None, sdkconfig: Optional[str] = None):
# CLI options to filter the test cases
self.target = target
self.sdkconfig = sdkconfig
@pytest.hookimpl(tryfirst=True)
def pytest_sessionstart(self, session: Session) -> None:
if self.target:
self.target = self.target.lower()
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
@ -182,19 +203,19 @@ def pytest_collection_modifyitems(config: Config, items: List[Function]) -> None
item.add_marker(_target)
# filter all the test cases with "--target"
items[:] = [item for item in items if target in item_marker_names(item)]
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 config.getoption('sdkconfig'):
if self.sdkconfig:
items[:] = [
item
for item in items
if _get_param_config(item) == config.getoption('sdkconfig')
if _get_param_config(item) == self.sdkconfig
]
@pytest.hookimpl(trylast=True)
def pytest_runtest_teardown(item: Function) -> None:
@pytest.hookimpl(trylast=True)
def pytest_runtest_teardown(self, item: Function) -> None:
"""
Format the test case generated junit reports
"""