__init__.py 6.4 KB

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