tests: replace CLI-dependent xfail/skipif conditions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

# Conflicts:
#	docs/en/contribute/esp-idf-tests-with-pytest.rst
#	docs/zh_CN/contribute/esp-idf-tests-with-pytest.rst
#	tools/test_apps/system/panic/pytest_panic.py
This commit is contained in:
Fu Hanxi
2026-08-19 15:42:18 +02:00
parent 88b2ffcde6
commit 3627752631
4 changed files with 89 additions and 24 deletions

View File

@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded import Dut
@@ -20,15 +20,14 @@ def test_esp_event(dut: Dut) -> None:
@pytest.mark.qemu
@pytest.mark.xfail('config.getvalue("target") == "esp32c3"', reason='Unstable on QEMU, needs investigation')
@pytest.mark.parametrize(
'config',
@idf_parametrize(
'config,target,markers',
[
'defaults',
('defaults', 'esp32'),
('defaults', 'esp32c3', (pytest.mark.xfail(reason='Unstable on QEMU, needs investigation'),)),
],
indirect=True,
indirect=['config', 'target'],
)
@idf_parametrize('target', ['esp32', 'esp32c3'], indirect=['target'])
def test_esp_event_qemu(dut: Dut) -> None:
for case in dut.test_menu:
if 'qemu-ignore' not in case.groups and not case.is_ignored and case.type == 'normal':

View File

@@ -702,13 +702,35 @@ Now you may mark this test case with marker `xfail <https://docs.pytest.org/en/l
This code example is taken from :idf_file:`pytest_panic.py <tools/test_apps/system/panic/pytest_panic.py>`
.. attention::
Avoid using a string condition, e.g., ``@pytest.mark.xfail('config.getvalue("target") == "esp32s2"', ...)``. This condition is evaluated against the session-level CLI ``--target`` option, not against the actual target resolved for a given parametrized test instance, so it can silently mismatch when the same test is parametrized over multiple targets and produce unreliable results.
Instead, attach the ``xfail`` marker directly to the specific parametrized value passed to ``idf_parametrize`` (see :ref:`Same App With Different Running Environments <pytest-same-app-different-running-environments>`), so the marker is tied to that value at collection time.
This code example is taken from :idf_file:`pytest_panic.py <tools/test_apps/system/panic/pytest_panic.py>`
.. code-block:: python
@pytest.mark.xfail('config.getvalue("target") == "esp32s2"', reason='raised IllegalInstruction instead')
def test_cache_error(dut: PanicTestDut, config: str, test_func_name: str) -> None:
@pytest.mark.generic
@idf_parametrize(
'config,target,markers',
[
(
'memprot_esp32s2',
'esp32s2',
pytest.mark.xfail(reason='Incorrect panic reason may be observed', run=False),
),
('memprot_esp32c3', 'esp32c3'),
],
indirect=['config', 'target'],
)
def test_cache_error(dut: PanicTestDut, test_func_name: str) -> None:
This marker means that test is a known failure on the ESP32-S2.
If a test is only ever parametrized with a single target, there is no ambiguity, and you can use an unconditional ``@pytest.mark.xfail(reason=..., run=False)`` instead.
Mark Nightly Run Test Cases
---------------------------

View File

@@ -702,13 +702,35 @@ Pytest 使用技巧
以下代码来自 :idf_file:`pytest_panic.py <tools/test_apps/system/panic/pytest_panic.py>`
.. attention::
请避免使用字符串条件,例如 ``@pytest.mark.xfail('config.getvalue("target") == "esp32s2"', ...)``。该条件是在运行时根据 session 级别的 CLI ``--target`` 参数求值的,而不是针对某个具体参数化测试实例实际解析出的 target 值,因此当同一测试针对多个 target 参数化时,该条件可能被错误匹配,导致结果不可靠。
推荐做法是通过 ``idf_parametrize`` 传入的具体参数化取值直接附加 ``xfail`` marker更复杂的示例请参阅 :ref:`在不同运行环境中运行相同的应用程序 <pytest-same-app-different-running-environments>` 小节,这样该 marker 在 collection 阶段就与该取值绑定。
以下代码来自 :idf_file:`pytest_panic.py <tools/test_apps/system/panic/pytest_panic.py>`
.. code-block:: python
@pytest.mark.xfail('config.getvalue("target") == "esp32s2"', reason='raised IllegalInstruction instead')
def test_cache_error(dut: PanicTestDut, config: str, test_func_name: str) -> None:
@pytest.mark.generic
@idf_parametrize(
'config,target,markers',
[
(
'memprot_esp32s2',
'esp32s2',
pytest.mark.xfail(reason='Incorrect panic reason may be observed', run=False),
),
('memprot_esp32c3', 'esp32c3'),
],
indirect=['config', 'target'],
)
def test_cache_error(dut: PanicTestDut, test_func_name: str) -> None:
这一 marker 表示该测试在 ESP32-S2 上是一个已知失败。
如果一个测试始终只针对单个 target 参数化,则不存在歧义,此时可直接使用无条件的 ``@pytest.mark.xfail(reason=..., run=False)``
标记夜间运行的测试用例
---------------------------

View File

@@ -27,6 +27,16 @@ TARGETS_ALL = TARGETS_XTENSA + TARGETS_RISCV
# Some tests only run on dual-core targets, they use the config below.
TARGETS_DUAL_CORE = TARGETS_XTENSA_DUAL_CORE + TARGETS_RISCV_DUAL_CORE
def configs_with_esp32s2_xfail(
configs: Sequence[tuple[str, str]], reason: str
) -> list[tuple[str, str] | tuple[str, str, Any]]:
return [
(config, target, pytest.mark.xfail(reason=reason, run=False)) if target == 'esp32s2' else (config, target)
for config, target in configs
]
CONFIGS = list(
itertools.chain(
itertools.product(
@@ -780,7 +790,7 @@ def test_dcache_read_violation(dut: PanicTestDut, test_func_name: str) -> None:
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@pytest.mark.generic
@pytest.mark.xfail(targets=['esp32s2'], reason='Incorrect panic reason may be observed', run=False)
@pytest.mark.xfail(reason='Incorrect panic reason may be observed', run=False)
@idf_parametrize('config, target', CONFIGS_MEMPROT_DCACHE, indirect=['config', 'target'])
def test_dcache_write_violation(dut: PanicTestDut, test_func_name: str) -> None:
dut.run_test_func(test_func_name)
@@ -920,11 +930,14 @@ def iram_reg4_write_violation(dut: PanicTestDut, test_func_name: str) -> None:
dut.expect_cpu_reset()
# TODO: IDF-6820: ESP32-S2 -> Fix incorrect panic reason: Unhandled debug exception
@pytest.mark.generic
@pytest.mark.xfail(targets=['esp32s2'], reason='Incorrect panic reason may be observed', run=False)
@pytest.mark.temp_skip_ci(targets=['esp32h21'], reason='lack of runners')
@idf_parametrize('config, target', CONFIGS_MEMPROT_IDRAM, indirect=['config', 'target'])
# TODO: IDF-6820: ESP32-S2 -> Fix incorrect panic reason: Unhandled debug exception
@idf_parametrize(
'config,target,markers',
configs_with_esp32s2_xfail(CONFIGS_MEMPROT_IDRAM, 'Incorrect panic reason may be observed'),
indirect=['config', 'target'],
)
def test_iram_reg4_write_violation(dut: PanicTestDut, test_func_name: str) -> None:
iram_reg_write_violation(dut, test_func_name)
@@ -952,11 +965,14 @@ def dram_reg1_execute_violation(dut: PanicTestDut, test_func_name: str) -> None:
dut.expect_cpu_reset()
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@pytest.mark.generic
@pytest.mark.xfail(targets=['esp32s2'], reason='Multiple panic reasons for the same test may surface', run=False)
@pytest.mark.temp_skip_ci(targets=['esp32h21'], reason='lack of runners')
@idf_parametrize('config, target', CONFIGS_MEMPROT_IDRAM, indirect=['config', 'target'])
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@idf_parametrize(
'config,target,markers',
configs_with_esp32s2_xfail(CONFIGS_MEMPROT_IDRAM, 'Multiple panic reasons for the same test may surface'),
indirect=['config', 'target'],
)
def test_dram_reg1_execute_violation(dut: PanicTestDut, test_func_name: str) -> None:
dram_reg1_execute_violation(dut, test_func_name)
@@ -983,11 +999,14 @@ def dram_reg2_execute_violation(dut: PanicTestDut, test_func_name: str) -> None:
dut.expect_cpu_reset()
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@pytest.mark.generic
@pytest.mark.xfail(targets=['esp32s2'], reason='Multiple panic reasons for the same test may surface', run=False)
@pytest.mark.temp_skip_ci(targets=['esp32h21'], reason='lack of runners')
@idf_parametrize('config, target', CONFIGS_MEMPROT_IDRAM, indirect=['config', 'target'])
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@idf_parametrize(
'config,target,markers',
configs_with_esp32s2_xfail(CONFIGS_MEMPROT_IDRAM, 'Multiple panic reasons for the same test may surface'),
indirect=['config', 'target'],
)
def test_dram_reg2_execute_violation(dut: PanicTestDut, test_func_name: str) -> None:
dram_reg2_execute_violation(dut, test_func_name)
@@ -1032,11 +1051,14 @@ def test_rtc_fast_reg2_execute_violation(dut: PanicTestDut, test_func_name: str)
dut.expect_cpu_reset()
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@pytest.mark.generic
@pytest.mark.xfail(targets=['esp32s2'], reason='Multiple panic reasons for the same test may surface', run=False)
@pytest.mark.temp_skip_ci(targets=['esp32h21'], reason='lack of runners')
@idf_parametrize('config, target', CONFIGS_MEMPROT_RTC_FAST_MEM, indirect=['config', 'target'])
# TODO: IDF-6820: ESP32-S2 -> Fix multiple panic reasons in different runs
@idf_parametrize(
'config,target,markers',
configs_with_esp32s2_xfail(CONFIGS_MEMPROT_RTC_FAST_MEM, 'Multiple panic reasons for the same test may surface'),
indirect=['config', 'target'],
)
def test_rtc_fast_reg3_execute_violation(dut: PanicTestDut, test_func_name: str) -> None:
dut.run_test_func(test_func_name)