unity_fixture_extras.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016-2021 Espressif Systems (Shanghai) Co. 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. /* IDF-specific additions to "Unity Fixture".
  15. * This file doesn't need to be included directly, it gets included into unity.h
  16. * through unity_config.h.
  17. */
  18. #pragma once
  19. #include "sdkconfig.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #if !defined(CONFIG_IDF_TARGET) || defined(CONFIG_IDF_TARGET_LINUX)
  24. #define UNITY_MAYBE_EXIT(rc) do { exit(rc); } while(0)
  25. #else
  26. #define UNITY_MAYBE_EXIT(rc) do { (void) rc; } while(0)
  27. #endif
  28. /* A shorthand for running all tests called from one function "func_", from the app_main function.
  29. * Use this when there is more than one test group.
  30. *
  31. * Example:
  32. *
  33. * #include "unity.h"
  34. * #include "unity_fixture.h"
  35. *
  36. * static_void run_all_tests(void) {
  37. * RUN_TEST_GROUP(group1); // test group defined in another file, e.g. test_group1.c
  38. * RUN_TEST_GROUP(group2); // test group defined in another file, e.g. test_group2.c
  39. * }
  40. *
  41. * void app_main(void) {
  42. * UNITY_MAIN_FUNC(run_all_tests);
  43. * }
  44. */
  45. #define UNITY_MAIN_FUNC(func_) do { \
  46. const char* argv[] = { "test", "-v" }; \
  47. const int argc = sizeof(argv)/sizeof(argv[0]); \
  48. int rc = UnityMain(argc, argv, func_); \
  49. printf("\nTests finished, rc=%d\n", rc); \
  50. UNITY_MAYBE_EXIT(rc); \
  51. } while(0)
  52. /* A shorthand for running one test group from the app_main function, when there is only
  53. * one test group and it is defined in the same file.
  54. *
  55. * Example:
  56. *
  57. * #include "unity.h"
  58. * #include "unity_fixture.h"
  59. *
  60. * TEST_GROUP(my_feature);
  61. * // also define TEST_SETUP, TEST_TEARDOWN, TESTs, TEST_GROUP_RUNNER
  62. *
  63. * void app_main(void) {
  64. * UNITY_MAIN(my_feature);
  65. * }
  66. */
  67. #define UNITY_MAIN(group_) UNITY_MAIN_FUNC(TEST_ ## group_ ## _GROUP_RUNNER)
  68. #ifdef __cplusplus
  69. }
  70. #endif