__init__.py 4.5 KB

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