diff --git a/examples/openthread/pytest_otbr.py b/examples/openthread/pytest_otbr.py index 700ab2e1344..aebb6ed6c91 100644 --- a/examples/openthread/pytest_otbr.py +++ b/examples/openthread/pytest_otbr.py @@ -10,6 +10,7 @@ import secrets import subprocess import threading import time +from typing import Generator from typing import Optional from typing import Tuple @@ -86,6 +87,25 @@ ESPPORT3 = os.getenv('ESPPORT3') PORT_MAPPING = {'ESPPORT1': 'esp32h2', 'ESPPORT2': 'esp32s3', 'ESPPORT3': 'esp32c6'} +@pytest.fixture(scope='module', autouse=True) +def erase_flash_after_all_cases() -> Generator[None, None, None]: + yield + + serial_ports = list(dict.fromkeys(filter(None, map(os.getenv, PORT_MAPPING)))) + failed_ports = [] + for serial_port in serial_ports: + command = ['python', '-m', 'esptool', '--port', serial_port, 'erase_flash'] + logging.info('Erasing flash on %s: %s', serial_port, ' '.join(command)) + result = subprocess.run(command, capture_output=True, text=True) + logging.info('Erase flash stdout on %s:\n%s', serial_port, result.stdout) + if result.stderr: + logging.info('Erase flash stderr on %s:\n%s', serial_port, result.stderr) + if result.returncode != 0: + failed_ports.append(serial_port) + + assert not failed_ports, f'Failed to erase flash on ports: {failed_ports}' + + # Case 1: Thread network formation and attaching @pytest.mark.openthread_br @pytest.mark.flaky(reruns=1, reruns_delay=5)