Runner.py 4.1 KB

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