utest.h 3.9 KB

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