[Concept,3/9] test: py: Add --persist flag to keep test artefacts

Message ID 20251229160611.3899708-4-sjg@u-boot.org
State New
Headers
Series test: Various improvements to unit-test infrastructure |

Commit Message

Simon Glass Dec. 29, 2025, 4:06 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

When iterating on C test code, the Python fixtures that create disk
images run each time, even though the images have not changed. This
slows down the development cycle unnecessarily.

Add a -P/--persist option to prevent cleanup of test-generated files
like disk images. This allows re-running C tests directly, without
re-running the Python fixture each time.

Update the ext4l test to respect the persist flag.

Co-developed-by: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 doc/develop/py_testing.rst          | 7 +++++++
 test/py/conftest.py                 | 3 +++
 test/py/tests/test_fs/test_ext4l.py | 4 ++--
 3 files changed, 12 insertions(+), 2 deletions(-)
  

Patch

diff --git a/doc/develop/py_testing.rst b/doc/develop/py_testing.rst
index c65de27369b..303c07040b3 100644
--- a/doc/develop/py_testing.rst
+++ b/doc/develop/py_testing.rst
@@ -246,6 +246,13 @@  Command-line options
   sets the directory used to store persistent test data. This is test data that
   may be re-used across test runs, such as file-system images.
 
+-P, --persist
+  prevents cleanup of test-generated files like disk images after the test run
+  completes. This is useful when iterating on C test code, allowing you to
+  re-run the C tests without re-running the Python fixture that creates the
+  test images. Note that this must be individually supported by each test, e.g.
+  with a check against u_boot_config.persist before removing images.
+
 --timing
   shows a histogram of test duration, at the end of the run. The columns are:
 
diff --git a/test/py/conftest.py b/test/py/conftest.py
index 247d4e19094..b79080af961 100644
--- a/test/py/conftest.py
+++ b/test/py/conftest.py
@@ -100,6 +100,8 @@  def pytest_addoption(parser):
         help="Assume that U-Boot is ready and don't wait for a prompt")
     parser.addoption('--timing', default=False, action='store_true',
                      help='Show info on test timing')
+    parser.addoption('-P', '--persist', default=False, action='store_true',
+                     help='Persist test artifacts (do not clean up after tests)')
 
 
 def run_build(config, source_dir, build_dir, board_type, log):
@@ -346,6 +348,7 @@  def pytest_configure(config):
     ubconfig.dtb = build_dir + '/arch/sandbox/dts/test.dtb'
     ubconfig.connection_ok = True
     ubconfig.timing = config.getoption('timing')
+    ubconfig.persist = config.getoption('persist')
     ubconfig.role = config.getoption('role')
     ubconfig.allow_exceptions = config.getoption('allow_exceptions')
 
diff --git a/test/py/tests/test_fs/test_ext4l.py b/test/py/tests/test_fs/test_ext4l.py
index 754c2cc69c4..3287f59fbe5 100644
--- a/test/py/tests/test_fs/test_ext4l.py
+++ b/test/py/tests/test_fs/test_ext4l.py
@@ -65,8 +65,8 @@  class TestExt4l:
 
         yield image_path
 
-        # Cleanup
-        if os.path.exists(image_path):
+        # Cleanup (skip if --persist flag is set)
+        if not u_boot_config.persist and os.path.exists(image_path):
             os.remove(image_path)
 
     def test_probe(self, ubman, ext4_image):