On Tue, Jul 29, 2025 at 4:14 PM Rae Moar rmoar@google.com wrote:
On Tue, Jul 29, 2025 at 3:37 PM Marie Zhussupova marievic@google.com wrote:
KUnit parameterized tests currently support two primary methods for getting parameters:
- Defining custom logic within a `generate_params` function.
- Using the KUNIT_ARRAY_PARAM and KUNIT_ARRAY_PARAM_DESC macros with pre-defined static arrays.
These methods present limitations when dealing with dynamically generated parameter arrays, or in scenarios where populating parameters sequentially via `generate_params` is inefficient or overly complex.
This patch addresses these limitations by adding a new `params_data` field to `struct kunit`, of the type `kunit_params`. The struct `kunit_params` is designed to store the parameter array itself, along with essential metadata including the parameter count, parameter size, and a `get_description` function for providing custom descriptions for individual parameters.
The `params_data` field can be populated by calling the new `kunit_register_params_array` macro from within a `param_init` function. By attaching the parameter array directly to the parent kunit test instance, these parameters can be iterated over in kunit_run_tests() behind the scenes.
This modification provides greater flexibility to the KUnit framework, allowing testers to easily register and utilize both dynamic and static parameter arrays.
Signed-off-by: Marie Zhussupova marievic@google.com
include/kunit/test.h | 54 ++++++++++++++++++++++++++++++++++++++++---- lib/kunit/test.c | 26 ++++++++++++++++++++- 2 files changed, 75 insertions(+), 5 deletions(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h index 4ba65dc35710..9143f0e22323 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -245,7 +245,8 @@ static inline char *kunit_status_to_ok_not_ok(enum kunit_status status) */ #define KUNIT_CASE_PARAM_WITH_INIT(test_name, gen_params, init, exit) \ { .run_case = test_name, .name = #test_name, \
.generate_params = gen_params, \
.generate_params = (gen_params) \
?: kunit_get_next_param_and_desc, \ .param_init = init, .param_exit = exit, \ .module_name = KBUILD_MODNAME}
@@ -294,6 +295,21 @@ struct kunit_suite_set { struct kunit_suite * const *end; };
+/* Stores the pointer to the parameter array and its metadata. */ +struct kunit_params {
/*
* Reference to the parameter array for the parameterized tests. This
* is NULL if a parameter array wasn't directly passed to the
* parent kunit struct via the kunit_register_params_array macro.
*/
const void *params;
/* Reference to a function that gets the description of a parameter. */
void (*get_description)(const void *param, char *desc);
int num_params;
size_t elem_size;
+};
/**
- struct kunit - represents a running instance of a test.
@@ -302,12 +318,14 @@ struct kunit_suite_set {
- @parent: for user to store data that they want to shared across
parameterized tests. Typically, the data is provided in
the param_init function (see &struct kunit_case).
- @params_data: for users to directly store the parameter array.
- Used to store information about the current context under which the test
- is running. Most of this data is private and should only be accessed
- indirectly via public functions; the two exceptions are @priv and @parent
- which can be used by the test writer to store arbitrary data or data that is
- available to all parameter test executions, respectively.
- indirectly via public functions. There are three exceptions to this: @priv,
- @parent, and @params_data. These members can be used by the test writer to
- store arbitrary data, data available to all parameter test executions, and
*/
- the parameter array, respectively.
struct kunit { void *priv; @@ -316,6 +334,8 @@ struct kunit { * during parameterized testing. */ struct kunit *parent;
/* Stores the params array and all data related to it. */
struct kunit_params params_data; /* private: internal use only. */ const char *name; /* Read only after initialization! */
@@ -386,6 +406,8 @@ void kunit_exec_list_tests(struct kunit_suite_set *suite_set, bool include_attr) struct kunit_suite_set kunit_merge_suite_sets(struct kunit_suite_set init_suite_set, struct kunit_suite_set suite_set);
+const void *kunit_get_next_param_and_desc(struct kunit *test, const void *prev, char *desc);
Hello!
Thanks for sending out this series! I will do a full review of it. For now, I noticed that I get an error when I try to run KUnit tests as modules. I get the following error: "ERROR: modpost: "kunit_get_next_param_and_desc" [lib/kunit/kunit-example-test.ko] undefined!". As a possible fix, I suggest moving the function definition into the header file and making it a static inline function.
Thanks! -Rae
Hello! Feel free to also use EXPORT_SYMBOL_GPL(). Either solution should work here. Thanks!