Skip to content

Instantly share code, notes, and snippets.

@arpi-r

arpi-r/param-as-test-case.diff Secret

Last active Nov 5, 2020
Embed
What would you like to do?
These are changes from the previous version (v4) of support for parameterized testing
diff --git a/include/kunit/test.h b/include/kunit/test.h
index ec2307ee9bb0..dc09e4ba01a0 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -149,6 +149,7 @@ struct kunit_case {
void (*run_case)(struct kunit *test);
const char *name;
void* (*generate_params)(void *prev);
+ void *param_value;
/* private: internal use only. */
bool success;
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 8ad908b61494..a936973552af 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -82,10 +82,16 @@ static void kunit_print_ok_not_ok(void *test_or_suite,
pr_info("%s %zd - %s\n",
kunit_status_to_string(is_ok),
test_number, description);
- else
- kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
- kunit_status_to_string(is_ok),
- test_number, description);
+ else {
+ if (!test->param_value)
+ kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s",
+ kunit_status_to_string(is_ok),
+ test_number, description);
+ else
+ kunit_log(KERN_INFO, test, KUNIT_SUBTEST_INDENT "%s %zd - %s %d",
+ kunit_status_to_string(is_ok),
+ test_number, description, test->param_index + 1);
+ }
}
bool kunit_suite_has_succeeded(struct kunit_suite *suite)
@@ -247,18 +253,7 @@ static void kunit_run_case_internal(struct kunit *test,
}
}
- if (!test_case->generate_params) {
- test_case->run_case(test);
- } else {
- test->param_value = test_case->generate_params(NULL);
- test->param_index = 0;
-
- while (test->param_value) {
- test_case->run_case(test);
- test->param_value = test_case->generate_params(test->param_value);
- test->param_index++;
- }
- }
+ test_case->run_case(test);
}
static void kunit_case_internal_cleanup(struct kunit *test)
@@ -350,24 +345,55 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
struct kunit_try_catch *try_catch;
struct kunit test;
- kunit_init_test(&test, test_case->name, test_case->log);
- try_catch = &test.try_catch;
-
- kunit_try_catch_init(try_catch,
- &test,
- kunit_try_run_case,
- kunit_catch_run_case);
- context.test = &test;
- context.suite = suite;
- context.test_case = test_case;
- kunit_try_catch_run(try_catch, &context);
-
- test_case->success = test.success;
-
- kunit_print_ok_not_ok(&test, true, test_case->success,
- kunit_test_case_num(suite, test_case),
- test_case->name);
-}
+ if (!test_case->generate_params) {
+ kunit_init_test(&test, test_case->name, test_case->log);
+ try_catch = &test.try_catch;
+ test.param_value = NULL;
+
+ kunit_try_catch_init(try_catch,
+ &test,
+ kunit_try_run_case,
+ kunit_catch_run_case);
+ context.test = &test;
+ context.suite = suite;
+ context.test_case = test_case;
+ kunit_try_catch_run(try_catch, &context);
+
+ test_case->success = test.success;
+
+ kunit_print_ok_not_ok(&test, true, test_case->success,
+ kunit_test_case_num(suite, test_case),
+ test_case->name);
+ } else {
+ test_case->param_value = test_case->generate_params(NULL);
+ int param_index = 0;
+
+ while (test_case->param_value) {
+ kunit_init_test(&test, test_case->name, test_case->log);
+ try_catch = &test.try_catch;
+ test.param_value = test_case->param_value;
+ test.param_index = param_index;
+
+ kunit_try_catch_init(try_catch,
+ &test,
+ kunit_try_run_case,
+ kunit_catch_run_case);
+ context.test = &test;
+ context.suite = suite;
+ context.test_case = test_case;
+ kunit_try_catch_run(try_catch, &context);
+
+ test_case->success = test.success;
+
+ kunit_print_ok_not_ok(&test, true, test_case->success,
+ kunit_test_case_num(suite, test_case),
+ test_case->name);
+
+ test_case->param_value = test_case->generate_params(test_case->param_value);
+ param_index++;
+ }
+ }
+ }
int kunit_run_tests(struct kunit_suite *suite)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment