Skip to content

Commit edbcdc1

Browse files
authored
Merge pull request #289 from horw/feat/dry_run_unity_test
2 parents 5a86810 + 25b6478 commit edbcdc1

File tree

2 files changed

+100
-28
lines changed

2 files changed

+100
-28
lines changed

pytest-embedded-idf/pytest_embedded_idf/unity_tester.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,34 @@ def run_single_board_case(self, name: str, reset: bool = False, timeout: float =
416416
else:
417417
raise ValueError(f'single-board test case {name} not found')
418418

419+
@staticmethod
420+
def _select_to_run(group, name, attributes, case_groups, case_name, case_attributes):
421+
def validate_group():
422+
for _or in group:
423+
for _and in _or:
424+
invert = _and.startswith('!')
425+
_and = _and.lstrip('!')
426+
result = _and in case_groups
427+
if invert:
428+
result = not result
429+
if not result:
430+
break
431+
else:
432+
return True
433+
434+
return False
435+
436+
if not group and not name and not attributes:
437+
return True
438+
if group and validate_group():
439+
return True
440+
if name and case_name in name:
441+
return True
442+
if attributes and all(case_attributes.get(k) == v for k, v in attributes.items()):
443+
return True
444+
445+
return False
446+
419447
def run_all_single_board_cases(
420448
self,
421449
group: t.Optional[t.Union[str, list]] = None,
@@ -424,6 +452,7 @@ def run_all_single_board_cases(
424452
run_ignore_cases: bool = False,
425453
name: t.Optional[t.Union[str, list]] = None,
426454
attributes: t.Optional[dict] = None,
455+
dry_run: bool = False,
427456
) -> None:
428457
"""
429458
Run all single board cases, including multi_stage cases, and normal cases
@@ -440,6 +469,8 @@ def run_all_single_board_cases(
440469
run_ignore_cases: Whether to run ignored test cases or not.
441470
name: test case name or a list of test case names to run.
442471
attributes: Dictionary of attributes to filter and run test cases.
472+
dry_run: If True, then just show a list of test case names without running them.
473+
(please set the logging level as INFO to see them)
443474
"""
444475
if group is None:
445476
group = []
@@ -450,38 +481,15 @@ def run_all_single_board_cases(
450481
if isinstance(name, str):
451482
name: t.List[str] = [name]
452483

453-
def validate_group(case_groups):
454-
if not group:
455-
return True
456-
457-
for _or in group:
458-
for _and in _or:
459-
invert = _and.startswith('!')
460-
_and = _and.lstrip('!')
461-
result = _and in case_groups
462-
if invert:
463-
result = not result
464-
if not result:
465-
break
466-
else:
467-
return True
468-
469-
return False
470-
471484
for case in self.test_menu:
472-
selected = False
473-
if not group and not name and not attributes:
474-
selected = True
475-
if group and validate_group(case.groups):
476-
selected = True
477-
if name and case.name in name:
478-
selected = True
479-
if attributes and all(case.attributes.get(k) == v for k, v in attributes.items()):
480-
selected = True
481-
485+
selected = self._select_to_run(group, name, attributes, case.groups, case.name, case.attributes)
482486
if not selected:
483487
continue
488+
484489
if not case.is_ignored or run_ignore_cases:
490+
if dry_run:
491+
logging.info('[dry run] %s | %s', case.index, case.name)
492+
continue
485493
if case.type == 'normal':
486494
self._run_normal_case(case, reset=reset, timeout=timeout)
487495
elif case.type == 'multi_stage':

pytest-embedded-idf/tests/test_idf.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,70 @@ def test_idf_hard_reset_and_expect(dut):
682682
result.assert_outcomes(passed=1)
683683

684684

685+
def test_select_to_run():
686+
from pytest_embedded_idf.unity_tester import IdfUnityDutMixin
687+
688+
assert IdfUnityDutMixin._select_to_run(
689+
None, None, None,
690+
None, None, None
691+
)
692+
693+
assert IdfUnityDutMixin._select_to_run(
694+
None, ['name_hello', 'name_world'], None,
695+
None, 'name_hello', None
696+
)
697+
698+
assert not IdfUnityDutMixin._select_to_run(
699+
None, ['name_hello', 'name_world'], None,
700+
None, 'name_hel', None
701+
)
702+
703+
assert IdfUnityDutMixin._select_to_run(
704+
None, None, {"red": 255},
705+
None, None, {"red": 255, "green": 10, "blue": 33}
706+
)
707+
708+
assert not IdfUnityDutMixin._select_to_run(
709+
None, None, {"red": 25},
710+
None, None, {"red": 255, "green": 10, "blue": 33}
711+
)
712+
713+
assert IdfUnityDutMixin._select_to_run(
714+
None, None, {"red": 255, "green": 10},
715+
None, None, {"red": 255, "green": 10, "blue": 33}
716+
)
717+
718+
assert not IdfUnityDutMixin._select_to_run(
719+
None, None, {"red": 255, "green": 0},
720+
None, None, {"red": 255, "green": 10, "blue": 33}
721+
)
722+
723+
assert IdfUnityDutMixin._select_to_run(
724+
[['hello']], None, None,
725+
['hello', 'world'], None, None
726+
)
727+
728+
assert not IdfUnityDutMixin._select_to_run(
729+
[['!hello']], None, None,
730+
['hello', 'world'], None, None
731+
)
732+
733+
assert not IdfUnityDutMixin._select_to_run(
734+
[['hello', '!world']], None, None,
735+
['hello', 'world'], None, None
736+
)
737+
738+
assert IdfUnityDutMixin._select_to_run(
739+
[['hello', '!world'], ['sun']], None, None,
740+
['hello', 'world', 'sun'], None, None
741+
)
742+
743+
assert IdfUnityDutMixin._select_to_run(
744+
[['hello', '!w']], None, None,
745+
['hello', 'world'], None, None
746+
)
747+
748+
685749
def test_dut_run_all_single_board_cases(testdir):
686750
testdir.makepyfile(r"""
687751
def test_dut_run_all_single_board_cases(dut):

0 commit comments

Comments
 (0)