1import argparse 2import os.path 3import shutil 4import sys 5 6from xdevice.__main__ import main_process 7from xdevice._core.report.result_reporter import ResultReporter 8from xml.dom.minidom import parse 9# import pandas as pd 10from datetime import datetime 11 12BASE_DIR = os.path.dirname(__file__) 13 14TEST_CASE_MUST = [ 15 'Launcher', 16 #'ProcessCheck', 17 #'APLCheck', 18 #'ACLCheck', 19] 20 21# 根据用例耗时分配的两台设备跑的用例,使两台设备跑的时间都差不多在2min 22TEST_CASE_DEVICE1 = [ 23 'SettingsWifi', 24 #'CrashCheck', 25 #'Photos', 26 #'Contacts', 27 #'Mms', 28 #'DistributedMusicPlayer' 29] 30 31TEST_CASE_DEVICE2 = [ 32 'Camera', 33 #'NotificationBar', 34 #'Note', 35] 36 37 38def get_test_result(report_path): 39 try: 40 if not os.path.exists(report_path): 41 return False 42 rst = ResultReporter.get_task_info_params(report_path) 43 unsuccessful_params = rst.get('unsuccessful_params') 44 for case, step in unsuccessful_params.items(): 45 if step: 46 return False 47 return True 48 except: 49 return False 50 51 52# def collect_test_result(report_path): 53# xml_report = os.path.join(report_path, 'summary_report.xml') 54# if not os.path.exists(xml_report): 55# return 56# timestamp = datetime.fromtimestamp(os.path.getmtime(xml_report)) 57# test_date = timestamp.strftime('%Y-%m-%d') 58# 59# try: 60# dom = parse(xml_report) 61# data = dom.documentElement 62# test_result = { 63# '用例名': [], 64# '测试结果': [], 65# '耗时': [], 66# '报错信息': [], 67# '报告路径': [], 68# } 69# testcases = data.getElementsByTagName('testsuite') 70# testcase_result = [] 71# for t in testcases: 72# module_name = t.getAttribute('modulename') 73# result_kind = t.getAttribute('result_kind') 74# time = t.getAttribute('time') 75# testcase = t.getElementsByTagName('testcase') 76# message = testcase[0].getAttribute('message') 77# line = (module_name, result_kind, time, message, xml_report, test_date) 78# if line not in testcase_result: 79# testcase_result.append(line) 80# # csv 81# test_result['用例名'].append(module_name) 82# test_result['测试结果'].append(result_kind) 83# test_result['耗时'].append(time) 84# test_result['报错信息'].append(message) 85# test_result['报告路径'].append(xml_report) 86# 87# df = pd.DataFrame(test_result) 88# 89# with open('D:\\smoke_result_{}.csv'.format(test_date), 'a', newline='') as f: 90# df.to_csv(f, header=f.tell() == 0, index=False, mode='a') 91# except: 92# pass 93# 94 95if __name__ == '__main__': 96 argv = sys.argv[1:] 97 parser = argparse.ArgumentParser(description='manual to this scription') 98 parser.add_argument('--config', type=str) 99 parser.add_argument('--test_num', type=str, default='1/1') 100 parser.add_argument('--tools_path', type=str) 101 parser.add_argument('--anwser_path', type=str) 102 parser.add_argument('--save_path', type=str) 103 parser.add_argument('--device_num', type=str) 104 parser.add_argument('--pr_url', type=str) 105 args = parser.parse_args() 106 107 new_cmd = 'run' 108 # 指定设备sn 109 if not args.device_num: 110 print("SmokeTest: End of check, test failed!") 111 sys.exit(98) 112 new_cmd += ' -sn {}'.format(args.device_num) 113 # 测试用例路径 114 tcpath = args.tools_path 115 new_cmd += ' -tcpath {}'.format(tcpath) 116 # 测试的设备编号,1/1表示只有一台设备;1/2表示第一台设备;2/2表示第二台设备 117 if args.test_num == '1/1': 118 new_cmd += ' -l {}'.format(';'.join(TEST_CASE_MUST + TEST_CASE_DEVICE1 + TEST_CASE_DEVICE2)) 119 elif args.test_num == '1/2': 120 new_cmd += ' -l {}'.format(';'.join(TEST_CASE_MUST + TEST_CASE_DEVICE1)) 121 elif args.test_num == '2/2': 122 new_cmd += ' -l {}'.format(';'.join(TEST_CASE_MUST + TEST_CASE_DEVICE2)) 123 # 指定报告生成路径 124 report_path = args.save_path 125 new_cmd += ' -rp {} -ta screenshot:true'.format(report_path) 126 # 测试资源路径 127 # respath = args.anwser_path 128 # new_cmd += ' -respath {}'.format(respath) 129 # shutil.rmtree(os.path.join(BASE_DIR, 'reports'), ignore_errors=True) 130 131 print('SmokeTest Begin >>>>>>>>>>>>') 132 main_process(new_cmd) 133 134 # print('SmokeTest collect test result >>>>>>>>>>>>') 135 # collect_test_result(report_path) 136 137 print('SmokeTest ending >>>>>>>>>>>>') 138 smoke_rst = get_test_result(report_path) 139 if smoke_rst: 140 print("SmokeTest: End of check, test succeeded!") 141 sys.exit(0) 142 print("SmokeTest: End of check, test failed!") 143 sys.exit(99) 144