Commit 4d869c1e49e5a276bd53f50a83e78999a25e2846
Committed by
Tom Rini
1 parent
b59670f2bd
Exists in
smarc_8mq_lf_v2020.04
and in
20 other branches
test: Add a command function for test execution
The logic to either iterate through a list of tests or pick a named test is common to at lest two test suits. Move this logic into a new function and call it from the environment tests. Signed-off-by: Simon Glass <sjg@chromium.org>
Showing 3 changed files with 42 additions and 18 deletions Side-by-side Diff
include/test/suites.h
| ... | ... | @@ -8,6 +8,22 @@ |
| 8 | 8 | #ifndef __TEST_SUITES_H__ |
| 9 | 9 | #define __TEST_SUITES_H__ |
| 10 | 10 | |
| 11 | +struct unit_test; | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * cmd_ut_category() - Run a category of unit tests | |
| 15 | + * | |
| 16 | + * @name: Category name | |
| 17 | + * @tests: List of tests to run | |
| 18 | + * @n_ents: Number of tests in @tests | |
| 19 | + * @argc: Argument count provided. Must be <= 1. If this is 1 then all | |
| 20 | + * tests are run, otherwise only the one named @argv[1] is run. | |
| 21 | + * @argv: Arguments: argv[1] is the test to run (if @argc >= 2) | |
| 22 | + * @return 0 if OK, CMD_RET_FAILURE on failure | |
| 23 | + */ | |
| 24 | +int cmd_ut_category(const char *name, struct unit_test *tests, int n_ents, | |
| 25 | + int argc, char * const argv[]); | |
| 26 | + | |
| 11 | 27 | int do_ut_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 12 | 28 | int do_ut_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 13 | 29 | int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
test/cmd_ut.c
| ... | ... | @@ -8,8 +8,33 @@ |
| 8 | 8 | #include <common.h> |
| 9 | 9 | #include <command.h> |
| 10 | 10 | #include <test/suites.h> |
| 11 | +#include <test/test.h> | |
| 11 | 12 | |
| 12 | 13 | static int do_ut_all(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 14 | + | |
| 15 | +int cmd_ut_category(const char *name, struct unit_test *tests, int n_ents, | |
| 16 | + int argc, char * const argv[]) | |
| 17 | +{ | |
| 18 | + struct unit_test_state uts = { .fail_count = 0 }; | |
| 19 | + struct unit_test *test; | |
| 20 | + | |
| 21 | + if (argc == 1) | |
| 22 | + printf("Running %d %s tests\n", n_ents, name); | |
| 23 | + | |
| 24 | + for (test = tests; test < tests + n_ents; test++) { | |
| 25 | + if (argc > 1 && strcmp(argv[1], test->name)) | |
| 26 | + continue; | |
| 27 | + printf("Test: %s\n", test->name); | |
| 28 | + | |
| 29 | + uts.start = mallinfo(); | |
| 30 | + | |
| 31 | + test->func(&uts); | |
| 32 | + } | |
| 33 | + | |
| 34 | + printf("Failures: %d\n", uts.fail_count); | |
| 35 | + | |
| 36 | + return uts.fail_count ? CMD_RET_FAILURE : 0; | |
| 37 | +} | |
| 13 | 38 | |
| 14 | 39 | static cmd_tbl_t cmd_ut_sub[] = { |
| 15 | 40 | U_BOOT_CMD_MKENT(all, CONFIG_SYS_MAXARGS, 1, do_ut_all, "", ""), |
test/env/cmd_ut_env.c
| ... | ... | @@ -15,24 +15,7 @@ |
| 15 | 15 | { |
| 16 | 16 | struct unit_test *tests = ll_entry_start(struct unit_test, env_test); |
| 17 | 17 | const int n_ents = ll_entry_count(struct unit_test, env_test); |
| 18 | - struct unit_test_state uts = { .fail_count = 0 }; | |
| 19 | - struct unit_test *test; | |
| 20 | 18 | |
| 21 | - if (argc == 1) | |
| 22 | - printf("Running %d environment tests\n", n_ents); | |
| 23 | - | |
| 24 | - for (test = tests; test < tests + n_ents; test++) { | |
| 25 | - if (argc > 1 && strcmp(argv[1], test->name)) | |
| 26 | - continue; | |
| 27 | - printf("Test: %s\n", test->name); | |
| 28 | - | |
| 29 | - uts.start = mallinfo(); | |
| 30 | - | |
| 31 | - test->func(&uts); | |
| 32 | - } | |
| 33 | - | |
| 34 | - printf("Failures: %d\n", uts.fail_count); | |
| 35 | - | |
| 36 | - return uts.fail_count ? CMD_RET_FAILURE : 0; | |
| 19 | + return cmd_ut_category("environment", tests, n_ents, argc, argv); | |
| 37 | 20 | } |