__init__.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 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, **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 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 log_performance(item, value):
  75. """
  76. do print performance with pre-defined format to console
  77. :param item: performance item name
  78. :param value: performance value
  79. """
  80. performance_msg = "[Performance][{}]: {}".format(item, value)
  81. Utility.console_log(performance_msg, "orange")
  82. # update to junit test report
  83. current_junit_case = TinyFW.JunitReport.get_current_test_case()
  84. current_junit_case.stdout += performance_msg + "\r\n"
  85. def check_performance(item, value):
  86. """
  87. check if idf performance meet pass standard
  88. :param item: performance item name
  89. :param value: performance item value
  90. :raise: AssertionError: if check fails
  91. """
  92. ret = True
  93. standard_value = 0
  94. idf_path = IDFApp.get_sdk_path()
  95. performance_file = os.path.join(idf_path, "components", "idf_test", "include", "idf_performance.h")
  96. if os.path.exists(performance_file):
  97. with open(performance_file, "r") as f:
  98. data = f.read()
  99. match = re.search(r"#define\s+IDF_PERFORMANCE_(MIN|MAX)_{}\s+([\d.]+)".format(item.upper()), data)
  100. if match:
  101. op = match.group(1)
  102. standard_value = float(match.group(2))
  103. if op == "MAX":
  104. ret = value <= standard_value
  105. else:
  106. ret = value >= standard_value
  107. if not ret:
  108. raise AssertionError("[Performance] {} value is {}, doesn't meet pass standard {}"
  109. .format(item, value, standard_value))