SearchCases.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. for i, test_function in enumerate(test_functions):
  40. print("\t{}. ".format(i + 1) + test_function.case_info["name"])
  41. return test_functions
  42. @classmethod
  43. def _search_test_case_files(cls, test_case, file_pattern):
  44. """ search all test case files recursively of a path """
  45. if not os.path.exists(test_case):
  46. raise OSError("test case path not exist")
  47. if os.path.isdir(test_case):
  48. test_case_files = []
  49. for root, _, file_names in os.walk(test_case):
  50. for filename in fnmatch.filter(file_names, file_pattern):
  51. test_case_files.append(os.path.join(root, filename))
  52. else:
  53. test_case_files = [test_case]
  54. return test_case_files
  55. @classmethod
  56. def replicate_case(cls, case):
  57. """
  58. Replicate cases according to its filter values.
  59. If one case has specified filter chip=(ESP32, ESP32C),
  60. it will create 2 cases, one for ESP32 and on for ESP32C.
  61. Once the cases are replicated, it's easy to filter those we want to execute.
  62. :param case: the original case
  63. :return: a list of replicated cases
  64. """
  65. replicate_config = []
  66. for key in case.case_info:
  67. if isinstance(case.case_info[key], (list, tuple)):
  68. replicate_config.append(key)
  69. def _replicate_for_key(case_list, replicate_key, replicate_list):
  70. case_out = []
  71. for _case in case_list:
  72. for value in replicate_list:
  73. new_case = copy.deepcopy(_case)
  74. new_case.case_info[replicate_key] = value
  75. case_out.append(new_case)
  76. return case_out
  77. replicated_cases = [case]
  78. for key in replicate_config:
  79. replicated_cases = _replicate_for_key(replicated_cases, key, case.case_info[key])
  80. return replicated_cases
  81. @classmethod
  82. def search_test_cases(cls, test_case, test_case_file_pattern=None):
  83. """
  84. search all test cases from a folder or file, and then do case replicate.
  85. :param test_case: test case file(s) path
  86. :return: a list of replicated test methods
  87. """
  88. test_case_files = cls._search_test_case_files(test_case, test_case_file_pattern or cls.TEST_CASE_FILE_PATTERN)
  89. test_cases = []
  90. for test_case_file in test_case_files:
  91. test_cases += cls._search_cases_from_file(test_case_file)
  92. # handle replicate cases
  93. test_case_out = []
  94. for case in test_cases:
  95. test_case_out += cls.replicate_case(case)
  96. return test_case_out