注册接口测试

1、单个接口测试#encoding=utf-8import requestsimport jsondata = {'username': 'test001', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}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、单个接口带断言#encoding=utf-8import requestsimport jsonimport repattern = re.compile(r"{'code': '00', 'userid': \d+}")data = {'username': 'test003', 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}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、使用随机参数#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': 'wulaoshi@qq.com'}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、使用文件的的唯一数字参数使用户唯一#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': 'wulaoshi@qq.com'}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、封装常用函数#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': 'wulaoshi@qq.com'}data = json.dumps(data)res = post_request(url,data)get_response(res)assert_response(res)6、配置数据和程序的分离Conf.pyurl = 'http://39.106.41.11'port = 8080path = "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': 'wulaoshi@qq.com'}data = json.dumps(data)res = post_request(request_url,data)get_response(res)assert_response(res)7、配置数据、测试数据和程序的分离Conf.pyurl = 'http://39.106.41.11'port = 8080path = "register"request_url = url + ":" + str(port) + "/" + path + "/"Data.txt{'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}|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() #数据从文件读出全部是字符串,需要获取原类型 data = eval(line.split("|")[0]) #这里是把{'username': username, 'password': 'wulaoshi12345', 、 #'email': 'wulaoshi@qq.com'}赋值给data,以上的username会自动传入 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)