forked from espressif/esp-idf
ci(pytest): move to class plugin
This commit is contained in:
51
conftest.py
51
conftest.py
@@ -147,18 +147,39 @@ 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])
|
||||||
|
|
||||||
|
|
||||||
|
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
|
# sort by file path and callspec.config
|
||||||
# implement like this since this is a limitation of pytest, couldn't get fixture values while collecting
|
# 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
|
# 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)
|
item.add_marker(_target)
|
||||||
|
|
||||||
# filter all the test cases with "--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"
|
# filter all the test cases with cli option "config"
|
||||||
if config.getoption('sdkconfig'):
|
if self.sdkconfig:
|
||||||
items[:] = [
|
items[:] = [
|
||||||
item
|
item
|
||||||
for item in items
|
for item in items
|
||||||
if _get_param_config(item) == config.getoption('sdkconfig')
|
if _get_param_config(item) == self.sdkconfig
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@pytest.hookimpl(trylast=True)
|
||||||
@pytest.hookimpl(trylast=True)
|
def pytest_runtest_teardown(self, item: Function) -> None:
|
||||||
def pytest_runtest_teardown(item: Function) -> None:
|
|
||||||
"""
|
"""
|
||||||
Format the test case generated junit reports
|
Format the test case generated junit reports
|
||||||
"""
|
"""
|
||||||
|
Reference in New Issue
Block a user