__init__.py 6.7 KB

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