esp-idf/tools/ci/python_packages/tiny_test_fw/bin/Runner.py

125 lines
4.4 KiB
Python
Raw Normal View History

2017-10-09 22:44:55 -04:00
# Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Command line interface to run test cases from a given path.
* search and run test cases of a given path
* config file which support to filter test cases and passing data to test case
Use ``python Runner.py test_case_path -c config_file -e env_config_file`` to run test cases.
"""
import argparse
2017-10-09 22:44:55 -04:00
import os
import sys
import threading
from fnmatch import fnmatch
2017-10-09 22:44:55 -04:00
from tiny_test_fw.TinyFW import JunitReport, set_default_config
from tiny_test_fw.Utility import CaseConfig, SearchCases, console_log
2017-10-09 22:44:55 -04:00
class Runner(threading.Thread):
"""
:param test_case_paths: test case file or folder
2017-10-09 22:44:55 -04:00
:param case_config: case config file, allow to filter test cases and pass data to test case
:param env_config_file: env config file
"""
def __init__(self, test_case_paths, case_config, env_config_file=None, known_failure_cases_file=None):
2017-10-09 22:44:55 -04:00
super(Runner, self).__init__()
self.setDaemon(True)
if case_config:
test_suite_name = os.path.splitext(os.path.basename(case_config))[0]
else:
test_suite_name = 'TestRunner'
set_default_config(env_config_file=env_config_file, test_suite_name=test_suite_name)
test_methods = SearchCases.Search.search_test_cases(test_case_paths)
self.test_cases = CaseConfig.Parser.apply_config(test_methods, case_config)
self.known_failure_cases = self._get_config_cases(known_failure_cases_file)
@staticmethod
def _get_config_cases(config_file):
res = set()
if not config_file or not os.path.isfile(config_file):
return res
for line in open(config_file).readlines():
if not line:
continue
if not line.strip():
continue
without_comments = line.split('#')[0].strip()
if without_comments:
res.add(without_comments)
return res
2017-10-09 22:44:55 -04:00
def run(self):
for case in self.test_cases:
case.run()
2018-12-04 07:46:48 -05:00
@staticmethod
def is_known_issue(tc_name, known_cases):
for case in known_cases:
if tc_name == case:
return True
if fnmatch(tc_name, case):
return True
return False
def get_test_result(self):
_res = True
console_log('Test Results:')
for tc in JunitReport.JUNIT_TEST_SUITE.test_cases:
if tc.failures:
if self.is_known_issue(tc.name, self.known_failure_cases):
console_log(' Known Failure: ' + tc.name, color='orange')
else:
console_log(' Test Fail: ' + tc.name, color='red')
_res = False
else:
console_log(' Test Succeed: ' + tc.name, color='green')
return _res
2017-10-09 22:44:55 -04:00
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('test_cases', nargs='+',
help='test case folders or files')
parser.add_argument('--case_config', '-c', default=None,
help='case filter/config file')
parser.add_argument('--env_config_file', '-e', default=None,
help='test env config file')
parser.add_argument('--known_failure_cases_file', default=None,
help='known failure cases file')
2017-10-09 22:44:55 -04:00
args = parser.parse_args()
test_cases = [os.path.join(os.getenv('IDF_PATH'), path)
if not os.path.isabs(path) else path for path in args.test_cases]
runner = Runner(test_cases, args.case_config, args.env_config_file, args.known_failure_cases_file)
2017-10-09 22:44:55 -04:00
runner.start()
while True:
try:
runner.join(1)
if not runner.is_alive():
2017-10-09 22:44:55 -04:00
break
except KeyboardInterrupt:
print('exit by Ctrl-C')
2017-10-09 22:44:55 -04:00
break
if not runner.get_test_result():
2017-10-09 22:44:55 -04:00
sys.exit(1)