Runner.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. """
  15. Command line interface to run test cases from a given path.
  16. * search and run test cases of a given path
  17. * config file which support to filter test cases and passing data to test case
  18. Use ``python Runner.py test_case_path -c config_file -e env_config_file`` to run test cases.
  19. """
  20. import argparse
  21. import os
  22. import sys
  23. import threading
  24. from fnmatch import fnmatch
  25. from tiny_test_fw.TinyFW import JunitReport, set_default_config
  26. from tiny_test_fw.Utility import CaseConfig, SearchCases, console_log
  27. class Runner(threading.Thread):
  28. """
  29. :param test_case_paths: test case file or folder
  30. :param case_config: case config file, allow to filter test cases and pass data to test case
  31. :param env_config_file: env config file
  32. """
  33. def __init__(self, test_case_paths, case_config, env_config_file=None, known_failure_cases_file=None):
  34. super(Runner, self).__init__()
  35. self.setDaemon(True)
  36. if case_config:
  37. test_suite_name = os.path.splitext(os.path.basename(case_config))[0]
  38. else:
  39. test_suite_name = 'TestRunner'
  40. set_default_config(env_config_file=env_config_file, test_suite_name=test_suite_name)
  41. test_methods = SearchCases.Search.search_test_cases(test_case_paths)
  42. self.test_cases = CaseConfig.Parser.apply_config(test_methods, case_config)
  43. self.known_failure_cases = self._get_config_cases(known_failure_cases_file)
  44. @staticmethod
  45. def _get_config_cases(config_file):
  46. res = set()
  47. if not config_file or not os.path.isfile(config_file):
  48. return res
  49. for line in open(config_file).readlines():
  50. if not line:
  51. continue
  52. if not line.strip():
  53. continue
  54. without_comments = line.split('#')[0].strip()
  55. if without_comments:
  56. res.add(without_comments)
  57. return res
  58. def run(self):
  59. for case in self.test_cases:
  60. case.run()
  61. @staticmethod
  62. def is_known_issue(tc_name, known_cases):
  63. for case in known_cases:
  64. if tc_name == case:
  65. return True
  66. if fnmatch(tc_name, case):
  67. return True
  68. return False
  69. def get_test_result(self):
  70. _res = True
  71. console_log('Test Results:')
  72. for tc in JunitReport.JUNIT_TEST_SUITE.test_cases:
  73. if tc.failures:
  74. if self.is_known_issue(tc.name, self.known_failure_cases):
  75. console_log(' Known Failure: ' + tc.name, color='orange')
  76. else:
  77. console_log(' Test Fail: ' + tc.name, color='red')
  78. _res = False
  79. else:
  80. console_log(' Test Succeed: ' + tc.name, color='green')
  81. return _res
  82. if __name__ == '__main__':
  83. parser = argparse.ArgumentParser()
  84. parser.add_argument('test_cases', nargs='+',
  85. help='test case folders or files')
  86. parser.add_argument('--case_config', '-c', default=None,
  87. help='case filter/config file')
  88. parser.add_argument('--env_config_file', '-e', default=None,
  89. help='test env config file')
  90. parser.add_argument('--known_failure_cases_file', default=None,
  91. help='known failure cases file')
  92. args = parser.parse_args()
  93. test_cases = [os.path.join(os.getenv('IDF_PATH'), path)
  94. if not os.path.isabs(path) else path for path in args.test_cases]
  95. runner = Runner(test_cases, args.case_config, args.env_config_file, args.known_failure_cases_file)
  96. runner.start()
  97. while True:
  98. try:
  99. runner.join(1)
  100. if not runner.is_alive():
  101. break
  102. except KeyboardInterrupt:
  103. print('exit by Ctrl-C')
  104. break
  105. if not runner.get_test_result():
  106. sys.exit(1)