Registration Interface Test
1. Single interface test # encoding = utf-8import requestsimport jsondata = {'username': 'test001', 'Password': 'wulaoshi12345 ', 'address ': '[email protected]'} DATA = JSON. dumps (data) RES = requests. post ('HTTP: // 39.106.41.11: 8080/register/', data) print (res. text) print (res. status_code) print (res. JSON () 2. Single interface with assertion # encoding = utf-8import requestsimport jsonimport repattern = Re. compile (R "{'code': '00', 'userid': \ D +}") Data = {'username': 'test003 ', 'Password ': 'wulaoshi12345', 'email ':' [email protected] '} DATA = JSON. dumps (data) RES = requests. post ('HTTP: // 39.106.41.11: 8080/register/', data) print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () assert pattern. match (STR (res. JSON () 3. Use the random parameter # encoding = utf-8import requestsimport JSON, randomimport re, stringusername = [String. ascii_letters [random. randint (0, 25)] For I in range (8)] username = "". join (username) pattern = Re. compile (R "{'code': '00', 'userid': \ D +}") Data = {'username': username, 'Password': 'wulaoshi12345 ', 'email ':' [email protected] '} DATA = JSON. dumps (data) RES = requests. post ('HTTP: // 39.106.41.11: 8080/register/', data) print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () assert pattern. match (STR (res. JSON () 4. Use the unique numeric parameter of the file to make the user unique # encoding = utf-8import requestsimport JSON, randomimport re, stringwith open ("E: \ Python \ c.txt ", "R +") as file_obj: username = file_obj.read (). strip () print (username) file_obj.seek (0, 0) file_obj.write (STR (INT (username) + 1) username = "hhq" + usernamepattern = Re. compile (R "{'code': '00', 'userid': \ D +}") Data = {'username': username, 'Password': 'wulaoshi12345 ', 'email ':' [email protected] '} DATA = JSON. dumps (data) RES = requests. post ('HTTP: // 39.106.41.11: 8080/register/', data) print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () assert pattern. match (STR (res. JSON () 5. encapsulate common functions # encoding = utf-8import requestsimport JSON, randomimport re, stringdef post_request (URL, data): res = requests. post (URL, data) return resdef GET_RESPONSE (RES): Print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () def assert_response (RES): pattern = Re. compile (R "{'code': '00', 'userid': \ D +}") assert pattern. match (STR (res. JSON () with open ("E: \ Python \ c.txt", "R +") as file_obj: username = file_obj.read (). strip () print (username) file_obj.seek (0, 0) file_obj.write (STR (INT (username) + 1) username = "hhq" + usernameurl = 'HTTP: // 39.106.41.11: 8080/register/'data = {'username': username, 'Password': 'wulaoshi12345 ', 'email': '[email protected]'} DATA = JSON. dumps (data) RES = post_request (URL, data) GET_RESPONSE (RES) assert_response (RES) 6. Configure the data and separate the conf program. pyurl = 'HTTP: // 39.106.41.11 'Port = 8080 Path = "register" request_url = URL + ":" + STR (port) + "/" + path + "/" # encoding = utf-8import requestsimport JSON, randomimport re, stringfrom conf import * def post_request (URL, data): res = requests. post (URL, data) return resdef GET_RESPONSE (RES): Print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () def assert_response (RES): pattern = Re. compile (R "{'code': '00', 'userid': \ D +}") assert pattern. match (STR (res. JSON () with open ("E: \ Python \ c.txt", "R +") as file_obj: username = file_obj.read (). strip () print (username) file_obj.seek (0, 0) file_obj.write (STR (INT (username) + 1) username = "hhq" + usernamedata = {'username': username, 'Password': 'wulaoshi12345 ', 'email': '[email protected]'} DATA = JSON. dumps (data) RES = post_request (request_url, data) GET_RESPONSE (RES) assert_response (RES) 7. configure data, test data, and program separation Conf. pyurl = 'HTTP: // 39.106.41.11 'Port = 8080 Path = "register" request_url = URL + ":" + STR (port) + "/" + path + "/" data.txt {'username': username, 'Password': 'wulaoshi12345 ', 'email ': '[email protected]'} | r "{'code': '00', 'userid': \ D +}" # encoding = utf-8import requestsimport JSON, randomimport re, stringfrom conf import * def post_request (URL, data): res = requests. post (URL, data) return resdef GET_RESPONSE (RES): Print (res. text) print (res. status_code) print (res. JSON () print (STR (res. JSON () def assert_response (Res, reg_pattern): pattern = Re. compile (reg_pattern) assert pattern. match (STR (res. JSON () with open ("E: \ Python \ c.txt", "R +") as file_obj: uniquenumber = file_obj.read (). strip () print (username) file_obj.seek (0, 0) file_obj.write (STR (INT (username) + 1) username = "hhq" + uniquenumberwith open ("E: \ Python \ data.txt "," R ") as FP: line = FP. readline (). strip () # Read all data from the file as strings and obtain the original data = eval (line. split ("|") [0]) # Here {'username': username, 'Password': 'wulaoshi12345 ', and # 'email ': '[email protected]'} is assigned to data. The above username will be automatically passed in reg_pattern = eval (line. split ("|") [1]) Data = JSON. dumps (data) print (data) RES = post_request (request_url, data) GET_RESPONSE (RES) assert_response (Res, reg_pattern)
Several organization forms of interface Testing