lib: tests: Add test suite init function
authorIvan Orlov <ivan.orlov0322@gmail.com>
Tue, 23 Apr 2024 15:52:43 +0000 (16:52 +0100)
committerAnup Patel <anup@brainfault.org>
Tue, 7 May 2024 05:57:13 +0000 (11:27 +0530)
Allow to define an init function for the test suite. It could help us
to initialize global variable once, and use them in multiple test cases
after the initialization.

For instance, if multiple test cases use the same atomic_t var, it
could be helpful to call ATOMIC_INIT once during the suite
initialization.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
include/sbi/sbi_unit_test.h
lib/sbi/tests/sbi_unit_test.c

index c63d900cbf3a6ffedc95bbf2f4e46996b196cb06..448c04864db2ff29c93acad2ba54d45139e38239 100644 (file)
@@ -19,6 +19,7 @@ struct sbiunit_test_case {
 
 struct sbiunit_test_suite {
        const char *name;
+       void (*init)(void);
        struct sbiunit_test_case *cases;
 };
 
@@ -34,6 +35,7 @@ struct sbiunit_test_suite {
 #define SBIUNIT_TEST_SUITE(suite_name, cases_arr)              \
        struct sbiunit_test_suite suite_name = {                \
                .name = #suite_name,                            \
+               .init = NULL,                                   \
                .cases = cases_arr                              \
        }
 
index 1987838c52b1c2f593fbc5d4d76aeb48da6c6c2e..c2a0be683a7190d8b37ea577077610ce622c4cd8 100644 (file)
@@ -17,6 +17,9 @@ static void run_test_suite(struct sbiunit_test_suite *suite)
 
        sbi_printf("## Running test suite: %s\n", suite->name);
 
+       if (suite->init)
+               suite->init();
+
        s_case = suite->cases;
        while (s_case->test_func) {
                s_case->test_func(s_case);