unity_test_runner.h 7.7 KB

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