python学习笔记(pict+requests+xml)
博主尝试了下更换python版本
之前很多脚本改正运行错误后、还是不能正常运行
忙会了半天还是没有成功
只好还原版本
所以下面的代码还没实际运行成功、先记录下
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 4 import subprocess 5 from createtest import CreateTest 6 import json 7 from xml.dom import minidom 8 import sys 9 10 old_name = "case_login.txt" 11 new_name = "case_list.txt" 12 cmd = "pict " + old_name + " > " + new_name 13 path = "E:\work\python project" 14 15 16 # noinspection PyTypeChecker,PyCallByClass 17 class CreateCase: 18 reload(sys) 19 sys.setdefaultencoding("utf-8") 20 21 def __init__(self): 22 pass 23 24 @classmethod 25 def case_cmd(cls): 26 subprocess.Popen(args=cmd, shell=True, stdout=subprocess.PIPE, cwd=path) 27 print "接口测试用例生成成功" 28 29 @classmethod 30 def case_list(cls): 31 case_list = [] 32 file_txt = open(new_name, "r+") 33 for line in file_txt.readlines(): 34 line_txt = line.replace("\n", '').split("\t") 35 case_list.append(line_txt) 36 return case_list 37 # txt读取拆分截取 38 39 @classmethod 40 def case_data(cls): 41 data_list = [] 42 for l in range(1, len(CreateCase.case_list())): 43 data_dict = {CreateCase.case_list()[0][0].decode("gbk"): CreateCase.case_list()[l][0].decode("gbk"), 44 CreateCase.case_list()[0][1].decode("gbk"): CreateCase.case_list()[l][1].decode("gbk")} 45 data_str = json.JSONEncoder().encode(data_dict) 46 data_json = json.loads(data_str) 47 data_list.append(data_json) 48 return data_list 49 # 创建字典转化json字符串、再转化成json格式 50 51 @classmethod 52 def case_result(cls): 53 post = "post" 54 url = "http://localhost:8081/swcw/back/sysLogin.action" 55 xml = minidom.Document() 56 xml.appendChild(xml.createComment("测试报告")) 57 caselist = xml.createElement("caselist") 58 xml.appendChild(caselist) 59 for i in range(0, len(CreateCase.case_data())): 60 case_result = CreateTest.test_api(post, url, CreateCase.case_data()[i]) 61 case_code = str(case_result.status_code) 62 case_name = str(CreateCase.case_data()[i]) 63 case_pattern = u"个人信息" 64 case_id = "swcw" + str(i) 65 try: 66 CreateTest.test_http(case_result.status_code) 67 except AttributeError: 68 pass 69 # 生成xml文件 70 case = xml.createElement("case") 71 case.setAttribute("id", case_id) 72 # 输入用例ID 73 74 name = xml.createElement("name") 75 name.appendChild(xml.createTextNode(case_name)) 76 # 输入用例名称 77 method = xml.createElement("method") 78 method.appendChild(xml.createTextNode(post)) 79 # 输入接口类型 80 code = xml.createElement("code") 81 code.appendChild((xml.createTextNode(case_code))) 82 # 输入用例返回状态码 83 result = xml.createElement("result") 84 result.appendChild(xml.createTextNode(CreateTest.test_result(case_pattern, case_result.text))) 85 # 输入用例测试结果 86 time = xml.createElement("time") 87 time.appendChild(xml.createTextNode(CreateTest.test_time())) 88 # 输入用例执行时间 89 90 case.appendChild(name) 91 case.appendChild(method) 92 case.appendChild(code) 93 case.appendChild(result) 94 case.appendChild(time) 95 96 caselist.appendChild(case) 97 # xml文件生成结束 98 filename = open(CreateTest.test_report(), "w+") 99 # 生成以当前时间命名的测试报告文件 100 xml.writexml(filename) 101 filename.close() 102 # 关闭文件 103 104 105 if __name__ == '__main__': 106 CreateCase.case_result()