Several organization forms of interface Testing

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.