utest.h 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-19     MurphyZhao   the first version
 */

#ifndef __UTEST_H__
#define __UTEST_H__

#include <rtthread.h>
#include "utest_log.h"
16
#include "utest_assert.h"
17

18 19 20 21 22 23 24 25 26 27
/**
 * utest_error
 * 
 * @brief Test result.
 * 
 * @member UTEST_PASSED Test success.
 * @member UTEST_FAILED Test failed.
 * @member UTEST_PASSED Test skipped.
 * 
*/
28 29 30 31 32 33 34 35
enum utest_error
{
    UTEST_PASSED  = 0,
    UTEST_FAILED  = 1,
    UTEST_SKIPPED = 2
};
typedef enum utest_error utest_err_e;

36 37 38 39 40 41 42 43 44 45
/**
 * utest
 * 
 * @brief utest data structure.
 * 
 * @member error      Error number from enum `utest_error`.
 * @member passed_num Total number of tests passed.
 * @member failed_num Total number of tests failed.
 * 
*/
46 47 48 49 50 51 52 53
struct utest
{
    utest_err_e error;
    uint32_t passed_num;
    uint32_t failed_num;
};
typedef struct utest *utest_t;

54 55 56 57 58 59 60
/**
 * utest_tc_export
 * 
 * @brief utest testcase data structure.
 *        Will export the data to `UtestTcTab` section in flash.
 * 
 * @member name        Testcase name.
61
 * @member run_timeout Testcase maximum test time (Time unit: seconds).
62 63 64 65 66
 * @member init        Necessary initialization before executing the test case function.
 * @member tc          Total number of tests failed.
 * @member cleanup     Total number of tests failed.
 * 
*/
67 68
struct utest_tc_export {
    const char  *name;
69
    uint32_t     run_timeout;
70 71 72 73 74 75
    rt_err_t   (*init)(void);
    void       (*tc)(void);
    rt_err_t   (*cleanup)(void);
};
typedef struct utest_tc_export *utest_tc_export_t;

76 77 78 79 80 81
/**
 * test_unit_func
 * 
 * @brief Unit test handler function pointer.
 * 
*/
82 83
typedef void (*test_unit_func)(void);

84 85 86 87 88 89 90 91 92 93 94 95
/**
 * utest_unit_run
 * 
 * @brief Unit test function executor.
 *        No need for the user to call this function directly
 * 
 * @param func           Unit test function.
 * @param unit_func_name Unit test function name.
 * 
 * @return void
 * 
*/
96
void utest_unit_run(test_unit_func func, const char *unit_func_name);
97 98 99 100 101 102 103 104 105 106 107 108

/**
 * utest_handle_get
 * 
 * @brief Get the utest data structure handle.
 *        No need for the user to call this function directly
 * 
 * @param void
 * 
 * @return utest_t type. (struct utest *)
 * 
*/
109 110
utest_t utest_handle_get(void);

111 112 113 114 115 116
/**
 * UTEST_NAME_MAX_LEN
 * 
 * @brief Testcase name maximum length.
 * 
*/
117 118
#define UTEST_NAME_MAX_LEN (128u)

119 120 121 122 123 124 125 126 127 128
/**
 * UTEST_TC_EXPORT
 * 
 * @brief Export testcase function to `UtestTcTab` section in flash.
 *        Used in application layer.
 * 
 * @param testcase The testcase function.
 * @param name     The testcase name.
 * @param init     The initialization function of the test case.
 * @param cleanup  The cleanup function of the test case.
129
 * @param timeout  Testcase maximum test time (Time unit: seconds).
130 131 132 133
 * 
 * @return None
 * 
*/
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 139 140 141 142
        name,                                                                  \
        timeout,                                                               \
        init,                                                                  \
        testcase,                                                              \
        cleanup                                                                \
143 144
    }

145 146 147 148 149 150 151 152 153 154 155
/**
 * UTEST_UNIT_RUN
 * 
 * @brief Unit test function executor.
 *        Used in `testcase` function in application.
 * 
 * @param test_unit_func Unit test function
 * 
 * @return None
 * 
*/
156 157
#define UTEST_UNIT_RUN(test_unit_func)                                         \
    utest_unit_run(test_unit_func, #test_unit_func);                           \
158 159
    if(utest_handle_get()->failed_num != 0) return;

160
#endif /* __UTEST_H__ */