Runner.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import TinyFW
  25. from 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. test_methods = SearchCases.Search.search_test_cases(test_case)
  36. self.test_cases = CaseConfig.Parser.apply_config(test_methods, case_config)
  37. self.test_result = True
  38. if case_config:
  39. test_suite_name = os.path.splitext(os.path.basename(case_config))[0]
  40. else:
  41. test_suite_name = "TestRunner"
  42. TinyFW.set_default_config(env_config_file=env_config_file, test_suite_name=test_suite_name)
  43. def run(self):
  44. for case in self.test_cases:
  45. self.test_result = self.test_result and case.run()
  46. if __name__ == '__main__':
  47. parser = argparse.ArgumentParser()
  48. parser.add_argument("test_case",
  49. help="test case folder or file")
  50. parser.add_argument("--case_config", "-c", default=None,
  51. help="case filter/config file")
  52. parser.add_argument("--env_config_file", "-e", default=None,
  53. help="test env config file")
  54. args = parser.parse_args()
  55. runner = Runner(args.test_case, args.case_config, args.env_config_file)
  56. runner.start()
  57. while True:
  58. try:
  59. runner.join(1)
  60. if not runner.isAlive():
  61. break
  62. except KeyboardInterrupt:
  63. print("exit by Ctrl-C")
  64. break
  65. if not runner.test_result:
  66. sys.exit(1)