Runner.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 os
  21. import sys
  22. import argparse
  23. import threading
  24. from tiny_test_fw import TinyFW
  25. from tiny_test_fw.Utility import SearchCases, CaseConfig
  26. class Runner(threading.Thread):
  27. """
  28. :param test_case: 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, case_config, env_config_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. TinyFW.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)
  41. self.test_cases = CaseConfig.Parser.apply_config(test_methods, case_config)
  42. self.test_result = []
  43. def run(self):
  44. for case in self.test_cases:
  45. result = case.run()
  46. self.test_result.append(result)
  47. def get_test_result(self):
  48. return self.test_result and all(self.test_result)
  49. if __name__ == '__main__':
  50. parser = argparse.ArgumentParser()
  51. parser.add_argument("test_case",
  52. help="test case folder or file")
  53. parser.add_argument("--case_config", "-c", default=None,
  54. help="case filter/config file")
  55. parser.add_argument("--env_config_file", "-e", default=None,
  56. help="test env config file")
  57. args = parser.parse_args()
  58. runner = Runner(args.test_case, args.case_config, args.env_config_file)
  59. runner.start()
  60. while True:
  61. try:
  62. runner.join(1)
  63. if not runner.isAlive():
  64. break
  65. except KeyboardInterrupt:
  66. print("exit by Ctrl-C")
  67. break
  68. if not runner.get_test_result():
  69. sys.exit(1)