Python learning notes (pict + requests + xml), pythonpict
The blogger tried to change the python version.
After many scripts correct running errors, they still cannot run normally.
Busy for half a day but still fail
You have to restore the version.
Therefore, the following code has not been successfully run and is recorded first.
1 #! /Usr/bin/env python 2 3 import subprocess 4 from project. createtest import CreateTest 5 import json 6 from xml. dom import minidom 7 8 txt_name = "case_login.txt" 9 excel_name = "case_list.txt" 10 cmd = "pict" + txt_name + ">" + excel_name11 path = "E: \ work \ python case \ project "12 case_list = [] 13 data_list = [] 14 15 16 # noinspection PyTypeChecker17 class CreateCase: 18 @ classmethod19 def case_cmd (cls): 20 subprocess. popen (args = cmd, shell = True, stdout = subprocess. PIPE, cwd = path) 21 print ("Interface Test Case generated successfully") 22 23 @ classmethod24 def case_data (cls): 25 file_txt = open (r 'e: \ work \ python case \ project \ case_list.txt ', "r +") 26 for line in file_txt.readlines (): 27 line = line. replace ('\ n ',''). split ("\ t") 28 case_list.append (line) 29 for l in range (1, len (case_list): 30 data_dict = {case_list [0] [0]: case_list [l] [0], case_list [0] [1]: case_list [l] [1]} 31 data_str = json. JSONEncoder (). encode (data_dict) 32 data = json. loads (data_str) 33 data_list.append (data) 34 return data_list35 # txt read splitting and truncation, create dictionary conversion json string, and then convert to json format 36 37 @ classmethod38 def case_result (cls ): 39 xml = minidom. document () 40 xml. appendChild (xml. createComment ("Test Report") 41 caselist = xml. createElement ("caselist") 42 xml. appendChild (caselist) 43 method = "post" 44 url = "http: // localhost: 8081/swcw/back/sysLogin. action "45 for l in range (0, len (CreateCase. case_data (): 46 case_result = CreateTest. test_api (method, url, CreateCase. case_data () [l]) 47 case_code = str (case_result.status_code) 48 try: 49 CreateTest. test_http (case_result.status_code) 50 TB t AttributeError: 51 pass52 53 # generate xml file 54 case = xml. createElement ("case") 55 case. setAttribute ("id", l) 56 # input case ID57 58 name = xml. createElement ("name") 59 name. appendChild (xml. createTextNode ("Login Test Case") 60 # enter the case name 61 method = xml. createElement ("method") 62 method. appendChild (xml. createTextNode ("post") 63 # Input Interface Type 64 code = xml. createElement ("code") 65 code. appendChild (xml. createTextNode (case_code) 66 # input case return status code 67 result = xml. createElement ("result") 68 result. appendChild (xml. createTextNode (CreateTest. test_result ("personal center", case_result.text) 69 # input test case test result 70 time = xml. createElement ("time") 71 time. appendChild (xml. createTextNode (CreateTest. test_time () 72 # input case execution time 73 74 case. appendChild (name) 75 case. appendChild (method) 76 case. appendChild (code) 77 case. appendChild (result) 78 case. appendChild (time) 79 80 caselist. appendChild (case) 81 # xml file generation ends 82 filename = open (CreateTest. test_report (), "w +") 83 # generate the test report file 84 xml named after the current time. writexml (filename) 85 filename. close () 86 # close the file 87 88 if _ name _ = '_ main _': 89 CreateCase. case_result ()