SearchCases.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. """ search test cases from a given file or path """
  15. import os
  16. import fnmatch
  17. import types
  18. import copy
  19. from . import load_source
  20. class Search(object):
  21. TEST_CASE_FILE_PATTERN = "*_test.py"
  22. @classmethod
  23. def _search_cases_from_file(cls, file_name):
  24. """ get test cases from test case .py file """
  25. print("Try to get cases from: " + file_name)
  26. test_functions = []
  27. try:
  28. mod = load_source(file_name)
  29. for func in [mod.__getattribute__(x) for x in dir(mod)
  30. if isinstance(mod.__getattribute__(x), types.FunctionType)]:
  31. try:
  32. # test method decorator will add test_method attribute to test function
  33. if func.test_method:
  34. test_functions.append(func)
  35. except AttributeError:
  36. continue
  37. except ImportError as e:
  38. print("ImportError: \r\n\tFile:" + file_name + "\r\n\tError:" + str(e))
  39. test_functions_out = []
  40. for case in test_functions:
  41. test_functions_out += cls.replicate_case(case)
  42. for i, test_function in enumerate(test_functions_out):
  43. print("\t{}. ".format(i + 1) + test_function.case_info["name"])
  44. return test_functions_out
  45. @classmethod
  46. def _search_test_case_files(cls, test_case, file_pattern):
  47. """ search all test case files recursively of a path """
  48. if not os.path.exists(test_case):
  49. raise OSError("test case path not exist")
  50. if os.path.isdir(test_case):
  51. test_case_files = []
  52. for root, _, file_names in os.walk(test_case):
  53. for filename in fnmatch.filter(file_names, file_pattern):
  54. test_case_files.append(os.path.join(root, filename))
  55. else:
  56. test_case_files = [test_case]
  57. return test_case_files
  58. @classmethod
  59. def replicate_case(cls, case):
  60. """
  61. Replicate cases according to its filter values.
  62. If one case has specified filter chip=(ESP32, ESP32C),
  63. it will create 2 cases, one for ESP32 and on for ESP32C.
  64. Once the cases are replicated, it's easy to filter those we want to execute.
  65. :param case: the original case
  66. :return: a list of replicated cases
  67. """
  68. replicate_config = []
  69. for key in case.case_info:
  70. if key == 'ci_target': # ci_target is used to filter target, should not be duplicated.
  71. continue
  72. if isinstance(case.case_info[key], (list, tuple)):
  73. replicate_config.append(key)
  74. def _replicate_for_key(cases, replicate_key, replicate_list):
  75. def deepcopy_func(f, name=None):
  76. fn = types.FunctionType(f.__code__, f.__globals__, name if name else f.__name__,
  77. f.__defaults__, f.__closure__)
  78. fn.__dict__.update(copy.deepcopy(f.__dict__))
  79. return fn
  80. case_out = []
  81. for inner_case in cases:
  82. for value in replicate_list:
  83. new_case = deepcopy_func(inner_case)
  84. new_case.case_info[replicate_key] = value
  85. case_out.append(new_case)
  86. return case_out
  87. replicated_cases = [case]
  88. while replicate_config:
  89. if not replicate_config:
  90. break
  91. key = replicate_config.pop()
  92. replicated_cases = _replicate_for_key(replicated_cases, key, case.case_info[key])
  93. return replicated_cases
  94. @classmethod
  95. def search_test_cases(cls, test_case, test_case_file_pattern=None):
  96. """
  97. search all test cases from a folder or file, and then do case replicate.
  98. :param test_case: test case file(s) path
  99. :return: a list of replicated test methods
  100. """
  101. test_case_files = cls._search_test_case_files(test_case, test_case_file_pattern or cls.TEST_CASE_FILE_PATTERN)
  102. test_cases = []
  103. for test_case_file in test_case_files:
  104. test_cases += cls._search_cases_from_file(test_case_file)
  105. return test_cases