Merge pull request #2322 from zhaojuntao/update-utest-0213

[components][utest] 增加线程模式、loop功能、通配符指定名字
This commit is contained in:
Bernard Xiong 2019-02-15 13:42:15 +08:00 committed by GitHub
commit c7a384b4f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 149 additions and 40 deletions

View File

@ -234,4 +234,13 @@ config RT_USING_UTEST
bool "Enable utest (RT-Thread test framework)"
default n
if RT_USING_UTEST
config UTEST_THR_STACK_SIZE
int "The utest thread stack size"
default 4096
config UTEST_THR_PRIORITY
int "The utest thread priority"
default 20
endif
endmenu

View File

@ -10,6 +10,8 @@
#include <rtthread.h>
#include <string.h>
#include <stdlib.h>
#include "utest.h"
#include <utest_log.h>
@ -32,9 +34,22 @@
#error "RT_CONSOLEBUF_SIZE is less than 256!"
#endif
#ifdef UTEST_THR_STACK_SIZE
#define UTEST_THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
#else
#define UTEST_THREAD_STACK_SIZE (4096)
#endif
#ifdef UTEST_THR_PRIORITY
#define UTEST_THREAD_PRIORITY UTEST_THR_PRIORITY
#else
#define UTEST_THREAD_PRIORITY FINSH_THREAD_PRIORITY
#endif
static rt_uint8_t utest_log_lv = UTEST_LOG_ALL;
static utest_tc_export_t tc_table = RT_NULL;
static rt_size_t tc_num;
static rt_uint32_t tc_loop;
static struct utest local_utest = {UTEST_PASSED, 0, 0};
#if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */
@ -104,18 +119,62 @@ static const char *file_basename(const char *file)
return (const char *)rst;
}
static int utest_help(void)
{
rt_kprintf("\n");
rt_kprintf("Command: utest_run\n");
rt_kprintf(" info: Execute test cases.\n");
rt_kprintf(" format: utest_run [-thread or -help] [testcase name] [loop num]\n");
rt_kprintf(" usage:\n");
rt_kprintf(" 1. utest_run\n");
rt_kprintf(" Do not specify a test case name. Run all test cases.\n");
rt_kprintf(" 2. utest_run -thread\n");
rt_kprintf(" Do not specify a test case name. Run all test cases in threaded mode.\n");
rt_kprintf(" 3. utest_run testcaseA\n");
rt_kprintf(" Run 'testcaseA'.\n");
rt_kprintf(" 4. utest_run testcaseA 10\n");
rt_kprintf(" Run 'testcaseA' ten times.\n");
rt_kprintf(" 5. utest_run -thread testcaseA\n");
rt_kprintf(" Run 'testcaseA' in threaded mode.\n");
rt_kprintf(" 6. utest_run -thread testcaseA 10\n");
rt_kprintf(" Run 'testcaseA' ten times in threaded mode.\n");
rt_kprintf(" 7. utest_run test*\n");
rt_kprintf(" support '*' wildcard. Run all test cases starting with 'test'.\n");
rt_kprintf(" 8. utest_run -help\n");
rt_kprintf(" Show utest help information\n");
rt_kprintf("\n");
return 0;
}
static void utest_run(const char *utest_name)
{
rt_size_t i = 0;
rt_size_t i;
rt_uint32_t index;
rt_bool_t is_find;
rt_thread_mdelay(1000);
for (index = 0; index < tc_loop; index ++)
{
i = 0;
is_find = RT_FALSE;
LOG_I("[==========] [ utest ] started");
while(i < tc_num)
{
if (utest_name && rt_strcmp(utest_name, tc_table[i].name))
if (utest_name)
{
int len = strlen(utest_name);
if (utest_name[len - 1] == '*')
{
len -= 1;
}
if (rt_memcmp(tc_table[i].name, utest_name, len) != 0)
{
i++;
continue;
}
}
is_find = RT_TRUE;
LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
if (tc_table[i].init != RT_NULL)
@ -158,29 +217,70 @@ __tc_continue:
i++;
}
if (i == tc_num && is_find == RT_FALSE)
{
LOG_I("[==========] [ utest ] Not find (%s)", utest_name);
LOG_I("[==========] [ utest ] finished");
break;
}
LOG_I("[==========] [ utest ] finished");
}
}
static void utest_testcase_run(int argc, char** argv)
{
char utest_name[UTEST_NAME_MAX_LEN];
void *thr_param = RT_NULL;
static char utest_name[UTEST_NAME_MAX_LEN];
rt_memset(utest_name, 0x0, sizeof(utest_name));
tc_loop = 1;
if (argc == 1)
{
utest_run(RT_NULL);
return;
}
else if (argc == 2)
else if (argc == 2 || argc == 3 || argc == 4)
{
if (rt_strcmp(argv[1], "-thread") == 0)
{
rt_thread_t tid = RT_NULL;
if (argc == 3 || argc == 4)
{
rt_strncpy(utest_name, argv[2], sizeof(utest_name) -1);
thr_param = (void*)utest_name;
if (argc == 4) tc_loop = atoi(argv[3]);
}
tid = rt_thread_create("utest",
(void (*)(void *))utest_run, thr_param,
UTEST_THREAD_STACK_SIZE, UTEST_THREAD_PRIORITY, 10);
if (tid != NULL)
{
rt_thread_startup(tid);
}
}
else if (rt_strcmp(argv[1], "-help") == 0)
{
utest_help();
}
else
{
rt_memset(utest_name, 0x0, sizeof(utest_name));
rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
if (argc == 3) tc_loop = atoi(argv[2]);
utest_run(utest_name);
}
}
else
{
LOG_E("[ error ] at (%s:%d), in param error.", __func__, __LINE__);
utest_help();
}
}
MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [testcase name]);
MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [-thread or -help] [testcase name] [loop num]);
utest_t utest_handle_get(void)
{