Skip to content

Commit

Permalink
feat: dry run single board unity test
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed May 7, 2024
1 parent 5a86810 commit 3299b72
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 27 deletions.
63 changes: 36 additions & 27 deletions pytest-embedded-idf/pytest_embedded_idf/unity_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,34 @@ def run_single_board_case(self, name: str, reset: bool = False, timeout: float =
else:
raise ValueError(f'single-board test case {name} not found')

@staticmethod
def _select_to_run(group, name, attributes, case_groups, case_name, case_attributes):
def validate_group():
for _or in group:
for _and in _or:
invert = _and.startswith('!')
_and = _and.lstrip('!')
result = _and in case_groups
if invert:
result = not result
if not result:
break
else:
return True

return False

if not group and not name and not attributes:
return True
if group and validate_group():
return True
if name and case_name in name:
return True
if attributes and all(case_attributes.get(k) == v for k, v in attributes.items()):
return True

return False

def run_all_single_board_cases(
self,
group: t.Optional[t.Union[str, list]] = None,
Expand All @@ -424,6 +452,7 @@ def run_all_single_board_cases(
run_ignore_cases: bool = False,
name: t.Optional[t.Union[str, list]] = None,
attributes: t.Optional[dict] = None,
dry_run: bool = False,
) -> None:
"""
Run all single board cases, including multi_stage cases, and normal cases
Expand All @@ -440,6 +469,8 @@ def run_all_single_board_cases(
run_ignore_cases: Whether to run ignored test cases or not.
name: test case name or a list of test case names to run.
attributes: Dictionary of attributes to filter and run test cases.
dry_run: If True, then just show a list of test case names without running them.
(please set the logging level as INFO to see them)
"""
if group is None:
group = []
Expand All @@ -450,37 +481,15 @@ def run_all_single_board_cases(
if isinstance(name, str):
name: t.List[str] = [name]

def validate_group(case_groups):
if not group:
return True

for _or in group:
for _and in _or:
invert = _and.startswith('!')
_and = _and.lstrip('!')
result = _and in case_groups
if invert:
result = not result
if not result:
break
else:
return True

return False

for case in self.test_menu:
selected = False
if not group and not name and not attributes:
selected = True
if group and validate_group(case.groups):
selected = True
if name and case.name in name:
selected = True
if attributes and all(case.attributes.get(k) == v for k, v in attributes.items()):
selected = True
selected = self._select_to_run(group, name, attributes, case.groups, case.name, case.attributes)

if not selected:
continue
if dry_run:
logging.info('[ignored] %s | %s', case.index, case.name)
continue

if not case.is_ignored or run_ignore_cases:
if case.type == 'normal':
self._run_normal_case(case, reset=reset, timeout=timeout)
Expand Down
64 changes: 64 additions & 0 deletions pytest-embedded-idf/tests/test_idf.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,70 @@ def test_idf_hard_reset_and_expect(dut):
result.assert_outcomes(passed=1)


def test_select_to_run():
from pytest_embedded_idf.unity_tester import IdfUnityDutMixin

assert IdfUnityDutMixin._select_to_run(
None, None, None,
None, None, None
)

assert IdfUnityDutMixin._select_to_run(
None, ['name_hello', 'name_world'], None,
None, 'name_hello', None
)

assert not IdfUnityDutMixin._select_to_run(
None, ['name_hello', 'name_world'], None,
None, 'name_hel', None
)

assert IdfUnityDutMixin._select_to_run(
None, None, {"red": 255},
None, None, {"red": 255, "green": 10, "blue": 33}
)

assert not IdfUnityDutMixin._select_to_run(
None, None, {"red": 25},
None, None, {"red": 255, "green": 10, "blue": 33}
)

assert IdfUnityDutMixin._select_to_run(
None, None, {"red": 255, "green": 10},
None, None, {"red": 255, "green": 10, "blue": 33}
)

assert not IdfUnityDutMixin._select_to_run(
None, None, {"red": 255, "green": 0},
None, None, {"red": 255, "green": 10, "blue": 33}
)

assert IdfUnityDutMixin._select_to_run(
[['hello']], None, None,
['hello', 'world'], None, None
)

assert not IdfUnityDutMixin._select_to_run(
[['!hello']], None, None,
['hello', 'world'], None, None
)

assert not IdfUnityDutMixin._select_to_run(
[['hello', '!world']], None, None,
['hello', 'world'], None, None
)

assert IdfUnityDutMixin._select_to_run(
[['hello', '!world'], ['sun']], None, None,
['hello', 'world', 'sun'], None, None
)

assert IdfUnityDutMixin._select_to_run(
[['hello', '!w']], None, None,
['hello', 'world'], None, None
)


def test_dut_run_all_single_board_cases(testdir):
testdir.makepyfile(r"""
def test_dut_run_all_single_board_cases(dut):
Expand Down

0 comments on commit 3299b72

Please sign in to comment.