Skip to content

Commit a442a7d

Browse files
committed
feat: added skip_if_soc marker for idf target
1 parent e299b26 commit a442a7d

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

pytest-embedded-idf/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ requires-python = ">=3.7"
3333
dependencies = [
3434
"pytest-embedded~=1.12.1",
3535
"esp-idf-panic-decoder",
36+
"esp-bool-parser>=0.1.2,<1"
3637
]
3738

3839
[project.optional-dependencies]

pytest-embedded-idf/tests/test_idf.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,3 +987,54 @@ def test_python_case(dut):
987987
assert junit_report[0].attrib['is_unity_case'] == '0' # Python test case
988988
for testcase in junit_report[1:]:
989989
assert testcase.attrib['is_unity_case'] == '1' # Other test cases
990+
991+
992+
def test_skip_if_soc(testdir):
993+
EMBEDDED_SERVICES = ['--embedded-services', 'esp,idf']
994+
995+
def run_pytest_with_target(target):
996+
count = len(target.split('|'))
997+
return testdir.runpytest(*EMBEDDED_SERVICES, '--target', target, '--count', count)
998+
999+
testdir.makepyfile("""
1000+
import pytest
1001+
from esp_bool_parser.constants import SUPPORTED_TARGETS
1002+
1003+
@pytest.mark.skip_if_soc("SOC_ULP_LP_UART_SUPPORTED == 1")
1004+
@pytest.mark.parametrize('target', ['esp32', 'esp32s3', 'esp32c6'], indirect=True)
1005+
def test_lp_uart_wakeup():
1006+
pass
1007+
1008+
@pytest.mark.skip_if_soc("SOC_BLE_SUPPORTED == 1")
1009+
@pytest.mark.parametrize('target', SUPPORTED_TARGETS, indirect=True)
1010+
def test_ble():
1011+
pass
1012+
1013+
@pytest.mark.skip_if_soc("SOC_ADC_SAMPLE_FREQ_THRES_HIGH == 83333")
1014+
@pytest.mark.parametrize('target', SUPPORTED_TARGETS, indirect=True)
1015+
def test_adc():
1016+
pass
1017+
1018+
""")
1019+
1020+
result = testdir.runpytest('-s', *EMBEDDED_SERVICES)
1021+
result.assert_outcomes(passed=14, failed=0, skipped=5)
1022+
1023+
testdir.makepyfile("""
1024+
import pytest
1025+
1026+
@pytest.mark.skip_if_soc("SOC_ULP_LP_UART_SUPPORTED == 1")
1027+
def test_from_args():
1028+
pass
1029+
1030+
""")
1031+
1032+
results = [
1033+
(run_pytest_with_target('esp32'), {'passed': 0, 'failed': 0, 'skipped': 1}),
1034+
(run_pytest_with_target('esp32c5'), {'passed': 1, 'failed': 0, 'skipped': 0}),
1035+
(run_pytest_with_target('auto'), {'passed': 1, 'failed': 0, 'skipped': 0}),
1036+
(run_pytest_with_target('esp32|esp32'), {'passed': 1, 'failed': 0, 'skipped': 0}),
1037+
]
1038+
1039+
for result, expected in results:
1040+
result.assert_outcomes(**expected)

pytest-embedded/pytest_embedded/plugin.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313
import tempfile
1414
import typing as t
15+
import warnings
1516
import xml.dom.minidom
1617
from collections import Counter
1718
from operator import itemgetter
@@ -1204,6 +1205,7 @@ def pytest_configure(config: Config) -> None:
12041205
add_target_as_marker=_str_bool(config.getoption('add_target_as_marker', False)),
12051206
)
12061207
config.pluginmanager.register(config.stash[_pytest_embedded_key])
1208+
config.addinivalue_line('markers', 'skip_if_soc')
12071209

12081210

12091211
def pytest_unconfigure(config: Config) -> None:
@@ -1252,7 +1254,39 @@ def _duplicate_items(items: t.List[_T]) -> t.List[_T]:
12521254
return duplicates
12531255

12541256
@pytest.hookimpl(hookwrapper=True, trylast=True)
1255-
def pytest_collection_modifyitems(self, items: t.List[Function]):
1257+
def pytest_collection_modifyitems(self, config: Config, items: t.List[Function]):
1258+
for item in items:
1259+
skip_marker = item.get_closest_marker('skip_if_soc')
1260+
if not skip_marker:
1261+
continue
1262+
1263+
from esp_bool_parser import parse_bool_expr
1264+
1265+
target = config.getoption('--target', None)
1266+
if hasattr(item, 'callspec'):
1267+
target = item.callspec.params.get('target', None)
1268+
if target == 'auto' or not isinstance(target, str):
1269+
warnings.warn(
1270+
f"Ignoring pytest.mark.skip_if_soc for test item '{item.originalname}': "
1271+
"Ensure that 'target' is included in the test's "
1272+
"@pytest.mark.parametrize when using 'skip_if_soc', "
1273+
'or provide the --target argument '
1274+
"when running tests (excluding 'auto' and multi-DUT configurations)."
1275+
)
1276+
continue
1277+
if '|' in target:
1278+
warnings.warn(
1279+
'Ignoring pytest.mark.skip_if_soc, '
1280+
"because multi-DUT tests do not support the 'skip_if_soc' marker. "
1281+
'Please adjust the test setup accordingly.'
1282+
)
1283+
continue
1284+
1285+
stm = parse_bool_expr(skip_marker.args[0])
1286+
if not stm.get_value(target, ''):
1287+
reason = f'Filtered by {skip_marker.args[0]}, for {target}.'
1288+
item.add_marker(pytest.mark.skip(reason=reason))
1289+
12561290
if self.add_target_as_marker:
12571291
for item in items:
12581292
item_target = item.callspec.getparam('target')

0 commit comments

Comments
 (0)