__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import TinyFW
  17. import Utility
  18. from IDF.IDFApp import IDFApp, Example, UT
  19. from IDF.IDFDUT import IDFDUT
  20. def idf_example_test(app=Example, dut=IDFDUT, chip="ESP32", module="examples", execution_time=1,
  21. level="example", erase_nvs=True, **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 kwargs: other keyword args
  32. :return: test method
  33. """
  34. try:
  35. # try to config the default behavior of erase nvs
  36. dut.ERASE_NVS = erase_nvs
  37. except AttributeError:
  38. pass
  39. return TinyFW.test_method(app=app, dut=dut, chip=chip, module=module,
  40. execution_time=execution_time, level=level, **kwargs)
  41. def idf_unit_test(app=UT, dut=IDFDUT, chip="ESP32", module="unit-test", execution_time=1,
  42. level="unit", erase_nvs=True, **kwargs):
  43. """
  44. decorator for testing idf unit tests (with default values for some keyword args).
  45. :param app: test application class
  46. :param dut: dut class
  47. :param chip: chip supported, string or tuple
  48. :param module: module, string
  49. :param execution_time: execution time in minutes, int
  50. :param level: test level, could be used to filter test cases, string
  51. :param erase_nvs: if need to erase_nvs in DUT.start_app()
  52. :param kwargs: other keyword args
  53. :return: test method
  54. """
  55. try:
  56. # try to config the default behavior of erase nvs
  57. dut.ERASE_NVS = erase_nvs
  58. except AttributeError:
  59. pass
  60. return TinyFW.test_method(app=app, dut=dut, chip=chip, module=module,
  61. execution_time=execution_time, level=level, **kwargs)
  62. def log_performance(item, value):
  63. """
  64. do print performance with pre-defined format to console
  65. :param item: performance item name
  66. :param value: performance value
  67. """
  68. performance_msg = "[Performance][{}]: {}".format(item, value)
  69. Utility.console_log(performance_msg, "orange")
  70. # update to junit test report
  71. current_junit_case = TinyFW.JunitReport.get_current_test_case()
  72. current_junit_case.stdout += performance_msg + "\r\n"
  73. def check_performance(item, value):
  74. """
  75. check if idf performance meet pass standard
  76. :param item: performance item name
  77. :param value: performance item value
  78. :raise: AssertionError: if check fails
  79. """
  80. ret = True
  81. standard_value = 0
  82. idf_path = IDFApp.get_sdk_path()
  83. performance_file = os.path.join(idf_path, "components", "idf_test", "include", "idf_performance.h")
  84. if os.path.exists(performance_file):
  85. with open(performance_file, "r") as f:
  86. data = f.read()
  87. match = re.search(r"#define\s+IDF_PERFORMANCE_(MIN|MAX)_{}\s+([\d.]+)".format(item.upper()), data)
  88. if match:
  89. op = match.group(1)
  90. standard_value = float(match.group(2))
  91. if op == "MAX":
  92. ret = value <= standard_value
  93. else:
  94. ret = value >= standard_value
  95. if not ret:
  96. raise AssertionError("[Performance] {} value is {}, doesn't meet pass standard {}"
  97. .format(item, value, standard_value))