]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: Support an init/uninit functions for test suites
authorSimon Glass <sjg@chromium.org>
Fri, 7 Feb 2025 18:30:38 +0000 (11:30 -0700)
committerTom Rini <trini@konsulko.com>
Wed, 12 Feb 2025 02:10:58 +0000 (20:10 -0600)
Some suites need things to be set up before they can run. Add a way to
declare an init function using the UNIT_TEST_INIT() macro. The init
function is just like any other test, but is always placed first so that
it runs before all the other test functions in the suite.

Add an uninit function as well, to clean up after the test.

Signed-off-by: Simon Glass <sjg@chromium.org>
include/test/test.h
test/test-main.c

index 877fda8d5aa8eea3725b432497f42b8414233ecc..0f2b68a5deed5bc6641db49db51aa42e0e56f15e 100644 (file)
@@ -100,6 +100,8 @@ enum ut_flags {
        UTF_ETH_BOOTDEV = BIT(9),       /* enable Ethernet bootdevs */
        UTF_SF_BOOTDEV  = BIT(10),      /* enable SPI flash bootdevs */
        UFT_BLOBLIST    = BIT(11),      /* test changes gd->bloblist */
+       UTF_INIT        = BIT(12),      /* test inits a suite */
+       UTF_UNINIT      = BIT(13),      /* test uninits a suite */
 };
 
 /**
@@ -147,6 +149,24 @@ struct unit_test {
                .func = _name,                                          \
        }
 
+/* init function for unit-test suite (the 'A' makes it first) */
+#define UNIT_TEST_INIT(_name, _flags, _suite)                          \
+       ll_entry_declare(struct unit_test, A ## _name, ut_ ## _suite) = {       \
+               .file = __FILE__,                                       \
+               .name = #_name,                                         \
+               .flags = (_flags) | UTF_INIT,                           \
+               .func = _name,                                          \
+       }
+
+/* uninit function for unit-test suite (the 'aaa' makes it last) */
+#define UNIT_TEST_UNINIT(_name, _flags, _suite)                                \
+       ll_entry_declare(struct unit_test, zzz ## _name, ut_ ## _suite) = { \
+               .file = __FILE__,                                       \
+               .name = #_name,                                         \
+               .flags = (_flags) | UTF_UNINIT,                         \
+               .func = _name,                                          \
+       }
+
 /* Get the start of a list of unit tests for a particular suite */
 #define UNIT_TEST_SUITE_START(_suite) \
        ll_entry_start(struct unit_test, ut_ ## _suite)
index 8c0d820032caae40723a3eb777284f01abe8195e..ee855bfe2ed5bd75ce546941feee10fba0337339 100644 (file)
@@ -620,7 +620,8 @@ static int ut_run_tests(struct unit_test_state *uts, const char *prefix,
                const char *test_name = test->name;
                int ret, i, old_fail_count;
 
-               if (!test_matches(prefix, test_name, select_name))
+               if (!(test->flags & (UTF_INIT | UTF_UNINIT)) &&
+                   !test_matches(prefix, test_name, select_name))
                        continue;
 
                if (test->flags & UTF_MANUAL) {