unity_test_runner.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. // This file gets included from unity.h via unity_internals.h via unity_config.h
  10. // It is inside #ifdef __cplusplus / extern "C" block, so we can
  11. // only use C features here
  12. // Define helpers to register test cases from multiple files
  13. #define UNITY_EXPAND2(a, b) a ## b
  14. #define UNITY_EXPAND(a, b) UNITY_EXPAND2(a, b)
  15. #define UNITY_TEST_UID(what) UNITY_EXPAND(what, __LINE__)
  16. #define UNITY_TEST_REG_HELPER reg_helper ## UNITY_TEST_UID
  17. #define UNITY_TEST_DESC_UID desc ## UNITY_TEST_UID
  18. // get count of __VA_ARGS__
  19. #define PP_NARG(...) \
  20. PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
  21. #define PP_NARG_(...) \
  22. PP_ARG_N(__VA_ARGS__)
  23. #define PP_ARG_N( \
  24. _1, _2, _3, _4, _5, _6, _7, _8, _9, N, ...) N
  25. #define PP_RSEQ_N() 9,8,7,6,5,4,3,2,1,0
  26. // support max 5 test func now
  27. #define FN_NAME_SET_1(a) {#a}
  28. #define FN_NAME_SET_2(a, b) {#a, #b}
  29. #define FN_NAME_SET_3(a, b, c) {#a, #b, #c}
  30. #define FN_NAME_SET_4(a, b, c, d) {#a, #b, #c, #d}
  31. #define FN_NAME_SET_5(a, b, c, d, e) {#a, #b, #c, #d, #e}
  32. #define FN_NAME_SET2(n) FN_NAME_SET_##n
  33. #define FN_NAME_SET(n, ...) FN_NAME_SET2(n)(__VA_ARGS__)
  34. #define UNITY_TEST_FN_SET(...) \
  35. static test_func UNITY_TEST_UID(test_functions)[] = {__VA_ARGS__}; \
  36. static const char* UNITY_TEST_UID(test_fn_name)[] = FN_NAME_SET(PP_NARG(__VA_ARGS__), __VA_ARGS__)
  37. typedef void (* test_func)(void);
  38. typedef struct test_desc_t
  39. {
  40. const char* name;
  41. const char* desc;
  42. test_func* fn;
  43. const char* file;
  44. int line;
  45. uint8_t test_fn_count;
  46. const char ** test_fn_name;
  47. struct test_desc_t* next;
  48. } test_desc_t;
  49. void unity_testcase_register(test_desc_t* desc);
  50. /* Test case macro, a-la CATCH framework.
  51. First argument is a free-form description,
  52. second argument is (by convention) a list of identifiers, each one in square brackets.
  53. Identifiers are used to group related tests, or tests with specific properties.
  54. Use like:
  55. TEST_CASE("Frobnicator forbnicates", "[frobnicator][rom]")
  56. {
  57. // test goes here
  58. }
  59. */
  60. #define TEST_CASE(name_, desc_) \
  61. static void UNITY_TEST_UID(test_func_) (void); \
  62. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
  63. { \
  64. static test_func test_fn_[] = {&UNITY_TEST_UID(test_func_)}; \
  65. static test_desc_t UNITY_TEST_UID(test_desc_) = { \
  66. .name = name_, \
  67. .desc = desc_, \
  68. .fn = test_fn_, \
  69. .file = __FILE__, \
  70. .line = __LINE__, \
  71. .test_fn_count = 1, \
  72. .test_fn_name = NULL, \
  73. .next = NULL \
  74. }; \
  75. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  76. }\
  77. static void UNITY_TEST_UID(test_func_) (void)
  78. /*
  79. * Multiple stages test cases will handle the case that test steps are separated by DUT reset.
  80. * e.g: we want to verify some function after SW reset, WDT reset or deep sleep reset.
  81. *
  82. * First argument is a free-form description,
  83. * second argument is (by convention) a list of identifiers, each one in square brackets.
  84. * subsequent arguments are names test functions separated by reset.
  85. * e.g:
  86. * TEST_CASE_MULTIPLE_STAGES("run light sleep after deep sleep","[sleep]", goto_deepsleep, light_sleep_after_deep_sleep_wakeup);
  87. * */
  88. #define TEST_CASE_MULTIPLE_STAGES(name_, desc_, ...) \
  89. UNITY_TEST_FN_SET(__VA_ARGS__); \
  90. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
  91. { \
  92. static test_desc_t UNITY_TEST_UID(test_desc_) = { \
  93. .name = name_, \
  94. .desc = desc_"[multi_stage]", \
  95. .fn = UNITY_TEST_UID(test_functions), \
  96. .file = __FILE__, \
  97. .line = __LINE__, \
  98. .test_fn_count = PP_NARG(__VA_ARGS__), \
  99. .test_fn_name = UNITY_TEST_UID(test_fn_name), \
  100. .next = NULL \
  101. }; \
  102. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  103. }
  104. /*
  105. * First argument is a free-form description,
  106. * second argument is (by convention) a list of identifiers, each one in square brackets.
  107. * subsequent arguments are names of test functions for different DUTs
  108. * e.g:
  109. * TEST_CASE_MULTIPLE_DEVICES("master and slave spi","[spi][test_env=UT_T2_1]", master_test, slave_test);
  110. * */
  111. #define TEST_CASE_MULTIPLE_DEVICES(name_, desc_, ...) \
  112. UNITY_TEST_FN_SET(__VA_ARGS__); \
  113. static void __attribute__((constructor)) UNITY_TEST_UID(test_reg_helper_) (void) \
  114. { \
  115. static test_desc_t UNITY_TEST_UID(test_desc_) = { \
  116. .name = name_, \
  117. .desc = desc_"[multi_device]", \
  118. .fn = UNITY_TEST_UID(test_functions), \
  119. .file = __FILE__, \
  120. .line = __LINE__, \
  121. .test_fn_count = PP_NARG(__VA_ARGS__), \
  122. .test_fn_name = UNITY_TEST_UID(test_fn_name), \
  123. .next = NULL \
  124. }; \
  125. unity_testcase_register( & UNITY_TEST_UID(test_desc_) ); \
  126. }
  127. /*
  128. Test case macro to be ignored in CI.
  129. Tests will still be built (to check for compile error) but not linked if IDF_CI_BUILD.
  130. */
  131. #if IDF_CI_BUILD
  132. #define TEST_CASE_CI_IGNORE(name_, desc_) \
  133. __attribute__((unused)) static void UNITY_TEST_UID(test_func_) (void)
  134. #else
  135. #define TEST_CASE_CI_IGNORE(name_, desc_) TEST_CASE(name_, desc_)
  136. #endif
  137. /**
  138. * Note: initialization of test_desc_t fields above has to be done exactly
  139. * in the same order as the fields are declared in the structure.
  140. * Otherwise the initializer will not be valid in C++ (which doesn't
  141. * support designated initializers). G++ can parse the syntax, but
  142. * field names are treated as annotations and don't affect initialization
  143. * order. Also make sure all the fields are initialized.
  144. */
  145. void unity_run_test_by_name(const char *name);
  146. void unity_run_test_by_index(int test_index);
  147. void unity_run_tests_by_tag(const char *tag, bool invert);
  148. void unity_run_all_tests(void);
  149. void unity_run_menu(void);
  150. int unity_get_test_count(void);
  151. bool unity_get_test_info(int test_index, test_desc_t* out_info);
  152. #include "sdkconfig.h" //to get IDF_TARGET_xxx
  153. #define CONFIG_IDF_TARGET_NA 0
  154. /*
  155. * This macro is to disable those tests and their callees that cannot be built or run temporarily
  156. * (needs update or runners).
  157. *
  158. * Usage:
  159. * ```
  160. * #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S2)
  161. * TEST_CASE("only for esp32", "")
  162. * {
  163. * }
  164. * #endif
  165. * ```
  166. */
  167. #define TEMPORARY_DISABLED_FOR_TARGETS(...) (_UNITY_DFT_10(__VA_ARGS__, NA, NA, NA, NA, NA, NA, NA, NA, NA))
  168. /*
  169. * This macro is to disable those tests and their callees that is totally impossible to run on the
  170. * specific targets. Usage same as TEMPORARY_DISABLED_FOR_TARGETS.
  171. */
  172. #define DISABLED_FOR_TARGETS(...) TEMPORARY_DISABLED_FOR_TARGETS(__VA_ARGS__)
  173. #define _UNITY_DFT_10(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_9(__VA_ARGS__))
  174. #define _UNITY_DFT_9(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_8(__VA_ARGS__))
  175. #define _UNITY_DFT_8(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_7(__VA_ARGS__))
  176. #define _UNITY_DFT_7(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_6(__VA_ARGS__))
  177. #define _UNITY_DFT_6(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_5(__VA_ARGS__))
  178. #define _UNITY_DFT_5(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_4(__VA_ARGS__))
  179. #define _UNITY_DFT_4(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_3(__VA_ARGS__))
  180. #define _UNITY_DFT_3(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_2(__VA_ARGS__))
  181. #define _UNITY_DFT_2(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET || _UNITY_DFT_1(__VA_ARGS__))
  182. #define _UNITY_DFT_1(TARGET, ...) (CONFIG_IDF_TARGET_##TARGET)