1 /* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date Author Notes 8 * 2018-11-19 MurphyZhao the first version 9 */ 10 11 #ifndef __UTEST_H__ 12 #define __UTEST_H__ 13 14 #include <rtthread.h> 15 #include "utest_log.h" 16 #include "utest_assert.h" 17 18 /** 19 * utest_error 20 * 21 * @brief Test result. 22 * 23 * @member UTEST_PASSED Test success. 24 * @member UTEST_FAILED Test failed. 25 * @member UTEST_PASSED Test skipped. 26 * 27 */ 28 enum utest_error 29 { 30 UTEST_PASSED = 0, 31 UTEST_FAILED = 1, 32 UTEST_SKIPPED = 2 33 }; 34 typedef enum utest_error utest_err_e; 35 36 /** 37 * utest 38 * 39 * @brief utest data structure. 40 * 41 * @member error Error number from enum `utest_error`. 42 * @member passed_num Total number of tests passed. 43 * @member failed_num Total number of tests failed. 44 * 45 */ 46 struct utest 47 { 48 utest_err_e error; 49 uint32_t passed_num; 50 uint32_t failed_num; 51 }; 52 typedef struct utest *utest_t; 53 54 /** 55 * utest_tc_export 56 * 57 * @brief utest testcase data structure. 58 * Will export the data to `UtestTcTab` section in flash. 59 * 60 * @member name Testcase name. 61 * @member run_timeout Testcase maximum test time (Time unit: seconds). 62 * @member init Necessary initialization before executing the test case function. 63 * @member tc Total number of tests failed. 64 * @member cleanup Total number of tests failed. 65 * 66 */ 67 struct utest_tc_export { 68 const char *name; 69 uint32_t run_timeout; 70 rt_err_t (*init)(void); 71 void (*tc)(void); 72 rt_err_t (*cleanup)(void); 73 }; 74 typedef struct utest_tc_export *utest_tc_export_t; 75 76 /** 77 * test_unit_func 78 * 79 * @brief Unit test handler function pointer. 80 * 81 */ 82 typedef void (*test_unit_func)(void); 83 84 /** 85 * utest_unit_run 86 * 87 * @brief Unit test function executor. 88 * No need for the user to call this function directly 89 * 90 * @param func Unit test function. 91 * @param unit_func_name Unit test function name. 92 * 93 * @return void 94 * 95 */ 96 void utest_unit_run(test_unit_func func, const char *unit_func_name); 97 98 /** 99 * utest_handle_get 100 * 101 * @brief Get the utest data structure handle. 102 * No need for the user to call this function directly 103 * 104 * @param void 105 * 106 * @return utest_t type. (struct utest *) 107 * 108 */ 109 utest_t utest_handle_get(void); 110 111 /** 112 * UTEST_NAME_MAX_LEN 113 * 114 * @brief Testcase name maximum length. 115 * 116 */ 117 #define UTEST_NAME_MAX_LEN (128u) 118 119 /** 120 * UTEST_TC_EXPORT 121 * 122 * @brief Export testcase function to `UtestTcTab` section in flash. 123 * Used in application layer. 124 * 125 * @param testcase The testcase function. 126 * @param name The testcase name. 127 * @param init The initialization function of the test case. 128 * @param cleanup The cleanup function of the test case. 129 * @param timeout Testcase maximum test time (Time unit: seconds). 130 * 131 * @return None 132 * 133 */ 134 #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ 135 RT_USED static const struct utest_tc_export _utest_testcase \ 136 SECTION("UtestTcTab") = \ 137 { \ 138 name, \ 139 timeout, \ 140 init, \ 141 testcase, \ 142 cleanup \ 143 } 144 145 /** 146 * UTEST_UNIT_RUN 147 * 148 * @brief Unit test function executor. 149 * Used in `testcase` function in application. 150 * 151 * @param test_unit_func Unit test function 152 * 153 * @return None 154 * 155 */ 156 #define UTEST_UNIT_RUN(test_unit_func) \ 157 utest_unit_run(test_unit_func, #test_unit_func); \ 158 if(utest_handle_get()->failed_num != 0) return; 159 160 #endif /* __UTEST_H__ */ 161