__init__.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2015-2017 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. import os
  15. import re
  16. from tiny_test_fw import TinyFW, Utility
  17. from .IDFApp import IDFApp, Example, LoadableElfExample, UT, TestApp # noqa: export all Apps for users
  18. from .IDFDUT import IDFDUT, ESP32DUT, ESP32S2DUT, ESP8266DUT, ESP32QEMUDUT # noqa: export DUTs for users
  19. def format_case_id(chip, case_name):
  20. return "{}.{}".format(chip, case_name)
  21. def idf_example_test(app=Example, dut=IDFDUT, chip="ESP32", module="examples", execution_time=1,
  22. level="example", erase_nvs=True, config_name=None, **kwargs):
  23. """
  24. decorator for testing idf examples (with default values for some keyword args).
  25. :param app: test application class
  26. :param dut: dut class
  27. :param chip: chip supported, string or tuple
  28. :param module: module, string
  29. :param execution_time: execution time in minutes, int
  30. :param level: test level, could be used to filter test cases, string
  31. :param erase_nvs: if need to erase_nvs in DUT.start_app()
  32. :param config_name: if specified, name of the app configuration
  33. :param kwargs: other keyword args
  34. :return: test method
  35. """
  36. try:
  37. # try to config the default behavior of erase nvs
  38. dut.ERASE_NVS = erase_nvs
  39. except AttributeError:
  40. pass
  41. original_method = TinyFW.test_method(app=app, dut=dut, chip=chip, module=module,
  42. execution_time=execution_time, level=level, **kwargs)
  43. def test(func):
  44. test_func = original_method(func)
  45. test_func.case_info["ID"] = format_case_id(chip, test_func.case_info["name"])
  46. return test_func
  47. return test
  48. def idf_unit_test(app=UT, dut=IDFDUT, chip="ESP32", module="unit-test", execution_time=1,
  49. level="unit", erase_nvs=True, **kwargs):
  50. """
  51. decorator for testing idf unit tests (with default values for some keyword args).
  52. :param app: test application class
  53. :param dut: dut class
  54. :param chip: chip supported, string or tuple
  55. :param module: module, string
  56. :param execution_time: execution time in minutes, int
  57. :param level: test level, could be used to filter test cases, string
  58. :param erase_nvs: if need to erase_nvs in DUT.start_app()
  59. :param kwargs: other keyword args
  60. :return: test method
  61. """
  62. try:
  63. # try to config the default behavior of erase nvs
  64. dut.ERASE_NVS = erase_nvs
  65. except AttributeError:
  66. pass
  67. original_method = TinyFW.test_method(app=app, dut=dut, chip=chip, module=module,
  68. execution_time=execution_time, level=level, **kwargs)
  69. def test(func):
  70. test_func = original_method(func)
  71. test_func.case_info["ID"] = format_case_id(chip, test_func.case_info["name"])
  72. return test_func
  73. return test
  74. def idf_custom_test(app=TestApp, dut=IDFDUT, chip="ESP32", module="misc", execution_time=1,
  75. level="integration", erase_nvs=True, config_name=None, group="test-apps", **kwargs):
  76. """
  77. decorator for idf custom tests (with default values for some keyword args).
  78. :param app: test application class
  79. :param dut: dut class
  80. :param chip: chip supported, string or tuple
  81. :param module: module, string
  82. :param execution_time: execution time in minutes, int
  83. :param level: test level, could be used to filter test cases, string
  84. :param erase_nvs: if need to erase_nvs in DUT.start_app()
  85. :param config_name: if specified, name of the app configuration
  86. :param group: identifier to group custom tests (unused for now, defaults to "test-apps")
  87. :param kwargs: other keyword args
  88. :return: test method
  89. """
  90. try:
  91. # try to config the default behavior of erase nvs
  92. dut.ERASE_NVS = erase_nvs
  93. except AttributeError:
  94. pass
  95. original_method = TinyFW.test_method(app=app, dut=dut, chip=chip, module=module,
  96. execution_time=execution_time, level=level, **kwargs)
  97. def test(func):
  98. test_func = original_method(func)
  99. test_func.case_info["ID"] = format_case_id(chip, test_func.case_info["name"])
  100. return test_func
  101. return test
  102. def log_performance(item, value):
  103. """
  104. do print performance with pre-defined format to console
  105. :param item: performance item name
  106. :param value: performance value
  107. """
  108. performance_msg = "[Performance][{}]: {}".format(item, value)
  109. Utility.console_log(performance_msg, "orange")
  110. # update to junit test report
  111. current_junit_case = TinyFW.JunitReport.get_current_test_case()
  112. current_junit_case.stdout += performance_msg + "\r\n"
  113. def check_performance(item, value):
  114. """
  115. check if idf performance meet pass standard
  116. :param item: performance item name
  117. :param value: performance item value
  118. :raise: AssertionError: if check fails
  119. """
  120. ret = True
  121. standard_value = 0
  122. idf_path = IDFApp.get_sdk_path()
  123. performance_file = os.path.join(idf_path, "components", "idf_test", "include", "idf_performance.h")
  124. if os.path.exists(performance_file):
  125. with open(performance_file, "r") as f:
  126. data = f.read()
  127. match = re.search(r"#define\s+IDF_PERFORMANCE_(MIN|MAX)_{}\s+([\d.]+)".format(item.upper()), data)
  128. if match:
  129. op = match.group(1)
  130. standard_value = float(match.group(2))
  131. if op == "MAX":
  132. ret = value <= standard_value
  133. else:
  134. ret = value >= standard_value
  135. if not ret:
  136. raise AssertionError("[Performance] {} value is {}, doesn't meet pass standard {}"
  137. .format(item, value, standard_value))