From: David Gow <davidgow@google.com>
To: Marie Zhussupova <marievic@google.com>,
marievictoria875@gmail.com, rmoar@google.com, shuah@kernel.org,
brendan.higgins@linux.dev
Cc: mark.rutland@arm.com, elver@google.com, dvyukov@google.com,
lucas.demarchi@intel.com, thomas.hellstrom@linux.intel.com,
rodrigo.vivi@intel.com, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com, kasan-dev@googlegroups.com,
intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org,
Stephen Rothwell <sfr@canb.auug.org.au>,
David Gow <davidgow@google.com>
Subject: [PATCH v4 1/7] kunit: Add parent kunit for parameterized test context
Date: Tue, 26 Aug 2025 17:13:31 +0800 [thread overview]
Message-ID: <20250826091341.1427123-2-davidgow@google.com> (raw)
In-Reply-To: <20250826091341.1427123-1-davidgow@google.com>
From: Marie Zhussupova <marievic@google.com>
Currently, KUnit parameterized tests lack a mechanism to share
resources across parameter runs because the same `struct kunit`
instance is cleaned up and reused for each run.
This patch introduces parameterized test context, enabling test
users to share resources between parameter runs. It also allows
setting up resources that need to be available for all parameter
runs only once, which is helpful in cases where setup is expensive.
To establish a parameterized test context, this patch adds a
parent pointer field to `struct kunit`. This allows resources added
to the parent `struct kunit` to be shared and accessible across all
parameter runs.
In kunit_run_tests(), the default `struct kunit` created is now
designated to act as the parameterized test context whenever a test
is parameterized.
Subsequently, a new `struct kunit` is made for each parameter run, and
its parent pointer is set to the `struct kunit` that holds the
parameterized test context.
Reviewed-by: David Gow <davidgow@google.com>
Reviewed-by: Rae Moar <rmoar@google.com>
Signed-off-by: Marie Zhussupova <marievic@google.com>
Signed-off-by: David Gow <davidgow@google.com>
---
No changes in v4:
v3: https://lore.kernel.org/linux-kselftest/20250815103604.3857930-2-marievic@google.com/
Changes in v3:
v2: https://lore.kernel.org/all/20250811221739.2694336-2-marievic@google.com/
- Commit message formatting.
Changes in v2:
v1: https://lore.kernel.org/all/20250729193647.3410634-2-marievic@google.com/
- Descriptions of the parent pointer in `struct kunit` were changed to
be more general, as it could be used to share resources not only
between parameter runs but also between test cases in the future.
- When printing parameter descriptions using test.param_index was changed
to param_test.param_index.
- kunit_cleanup(&test) in kunit_run_tests() was moved inside the
parameterized test check.
- The comments and the commit message were changed to reflect the
parameterized testing terminology. See the patch series cover letter
change log for the definitions.
---
include/kunit/test.h | 8 ++++++--
lib/kunit/test.c | 34 ++++++++++++++++++++--------------
2 files changed, 26 insertions(+), 16 deletions(-)
diff --git a/include/kunit/test.h b/include/kunit/test.h
index d958ee53050e..9766403afd56 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -268,14 +268,18 @@ struct kunit_suite_set {
*
* @priv: for user to store arbitrary data. Commonly used to pass data
* created in the init function (see &struct kunit_suite).
+ * @parent: reference to the parent context of type struct kunit that can
+ * be used for storing shared resources.
*
* 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 one exception is @priv which can be
- * used by the test writer to store arbitrary data.
+ * indirectly via public functions; the two exceptions are @priv and @parent
+ * which can be used by the test writer to store arbitrary data and access the
+ * parent context, respectively.
*/
struct kunit {
void *priv;
+ struct kunit *parent;
/* private: internal use only. */
const char *name; /* Read only after initialization! */
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index d2bfa331a2b1..587b5c51db58 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -647,6 +647,7 @@ int kunit_run_tests(struct kunit_suite *suite)
struct kunit_case *test_case;
struct kunit_result_stats suite_stats = { 0 };
struct kunit_result_stats total_stats = { 0 };
+ const void *curr_param;
/* Taint the kernel so we know we've run tests. */
add_taint(TAINT_TEST, LOCKDEP_STILL_OK);
@@ -679,37 +680,42 @@ int kunit_run_tests(struct kunit_suite *suite)
} else {
/* Get initial param. */
param_desc[0] = '\0';
- test.param_value = test_case->generate_params(NULL, param_desc);
+ /* TODO: Make generate_params try-catch */
+ curr_param = test_case->generate_params(NULL, param_desc);
test_case->status = KUNIT_SKIPPED;
kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
"KTAP version 1\n");
kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
"# Subtest: %s", test_case->name);
- while (test.param_value) {
- kunit_run_case_catch_errors(suite, test_case, &test);
+ while (curr_param) {
+ struct kunit param_test = {
+ .param_value = curr_param,
+ .param_index = ++test.param_index,
+ .parent = &test,
+ };
+ kunit_init_test(¶m_test, test_case->name, test_case->log);
+ kunit_run_case_catch_errors(suite, test_case, ¶m_test);
if (param_desc[0] == '\0') {
snprintf(param_desc, sizeof(param_desc),
- "param-%d", test.param_index);
+ "param-%d", param_test.param_index);
}
- kunit_print_ok_not_ok(&test, KUNIT_LEVEL_CASE_PARAM,
- test.status,
- test.param_index + 1,
+ kunit_print_ok_not_ok(¶m_test, KUNIT_LEVEL_CASE_PARAM,
+ param_test.status,
+ param_test.param_index,
param_desc,
- test.status_comment);
+ param_test.status_comment);
- kunit_update_stats(¶m_stats, test.status);
+ kunit_update_stats(¶m_stats, param_test.status);
/* Get next param. */
param_desc[0] = '\0';
- test.param_value = test_case->generate_params(test.param_value, param_desc);
- test.param_index++;
- test.status = KUNIT_SUCCESS;
- test.status_comment[0] = '\0';
- test.priv = NULL;
+ curr_param = test_case->generate_params(curr_param, param_desc);
}
+ /* TODO: Put this kunit_cleanup into a try-catch. */
+ kunit_cleanup(&test);
}
kunit_print_attr((void *)test_case, true, KUNIT_LEVEL_CASE);
--
2.51.0.261.g7ce5a0a67e-goog
next prev parent reply other threads:[~2025-08-26 9:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-26 9:13 [PATCH v4 0/7] kunit: Refactor and extend KUnit's parameterized testing framework David Gow
2025-08-26 9:13 ` David Gow [this message]
2025-08-26 9:13 ` [PATCH v4 2/7] kunit: Introduce param_init/exit for parameterized test context management David Gow
2025-08-26 9:13 ` [PATCH v4 3/7] kunit: Pass parameterized test context to generate_params() David Gow
2025-08-28 22:19 ` Lucas De Marchi
2025-08-26 9:13 ` [PATCH v4 4/7] kunit: Enable direct registration of parameter arrays to a KUnit test David Gow
2025-08-26 9:13 ` [PATCH v4 5/7] kunit: Add example parameterized test with shared resource management using the Resource API David Gow
2025-08-26 9:13 ` [PATCH v4 6/7] kunit: Add example parameterized test with direct dynamic parameter array setup David Gow
2025-08-26 9:13 ` [PATCH v4 7/7] Documentation: kunit: Document new parameterized test features David Gow
2025-08-28 16:56 ` [PATCH v4 0/7] kunit: Refactor and extend KUnit's parameterized testing framework Mark Rutland
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250826091341.1427123-2-davidgow@google.com \
--to=davidgow@google.com \
--cc=brendan.higgins@linux.dev \
--cc=dri-devel@lists.freedesktop.org \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=intel-xe@lists.freedesktop.org \
--cc=kasan-dev@googlegroups.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lucas.demarchi@intel.com \
--cc=marievic@google.com \
--cc=marievictoria875@gmail.com \
--cc=mark.rutland@arm.com \
--cc=rmoar@google.com \
--cc=rodrigo.vivi@intel.com \
--cc=sfr@canb.auug.org.au \
--cc=shuah@kernel.org \
--cc=thomas.hellstrom@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).