diff options
| author | Marie Zhussupova <marievic@google.com> | 2025-08-26 17:13:32 +0800 |
|---|---|---|
| committer | Shuah Khan <skhan@linuxfoundation.org> | 2025-08-26 23:36:03 -0600 |
| commit | 241423580e5e8d8b10b14b382379f4928b87be17 (patch) | |
| tree | 08d11182bd40fa40483938a7e30d6dc89930db94 /include/kunit | |
| parent | 4b59300ba4d2362b02c2a9077047bbabceea67d7 (diff) | |
kunit: Introduce param_init/exit for parameterized test context management
Add (*param_init) and (*param_exit) function pointers to
`struct kunit_case`. Users will be able to set them via the new
KUNIT_CASE_PARAM_WITH_INIT() macro.
param_init/exit will be invoked by kunit_run_tests() once before and once
after the parameterized test, respectively. They will receive the
`struct kunit` that holds the parameterized test context; facilitating
init and exit for shared state.
This patch also sets param_init/exit to None in rust/kernel/kunit.rs.
Link: https://lore.kernel.org/r/20250826091341.1427123-3-davidgow@google.com
Reviewed-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Marie Zhussupova <marievic@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'include/kunit')
| -rw-r--r-- | include/kunit/test.h | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/include/kunit/test.h b/include/kunit/test.h index 9766403afd56..fc8fd55b2dfb 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -92,6 +92,8 @@ struct kunit_attributes { * @name: the name of the test case. * @generate_params: the generator function for parameterized tests. * @attr: the attributes associated with the test + * @param_init: The init function to run before a parameterized test. + * @param_exit: The exit function to run after a parameterized test. * * A test case is a function with the signature, * ``void (*)(struct kunit *)`` @@ -128,6 +130,8 @@ struct kunit_case { const char *name; const void* (*generate_params)(const void *prev, char *desc); struct kunit_attributes attr; + int (*param_init)(struct kunit *test); + void (*param_exit)(struct kunit *test); /* private: internal use only. */ enum kunit_status status; @@ -219,6 +223,27 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) .attr = attributes, .module_name = KBUILD_MODNAME} /** + * KUNIT_CASE_PARAM_WITH_INIT - Define a parameterized KUnit test case with custom + * param_init() and param_exit() functions. + * @test_name: The function implementing the test case. + * @gen_params: The function to generate parameters for the test case. + * @init: A reference to the param_init() function to run before a parameterized test. + * @exit: A reference to the param_exit() function to run after a parameterized test. + * + * Provides the option to register param_init() and param_exit() functions. + * param_init/exit will be passed the parameterized test context and run once + * before and once after the parameterized test. The init function can be used + * to add resources to share between parameter runs, and any other setup logic. + * The exit function can be used to clean up resources that were not managed by + * the parameterized test, and any other teardown logic. + */ +#define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \ + { .run_case = test_name, .name = #test_name, \ + .generate_params = gen_params, \ + .param_init = init, .param_exit = exit, \ + .module_name = KBUILD_MODNAME} + +/** * struct kunit_suite - describes a related collection of &struct kunit_case * * @name: the name of the test. Purely informational. |