注冊接口測試
創(chuàng)新互聯(lián)是專業(yè)的安徽網(wǎng)站建設(shè)公司,安徽接單;提供網(wǎng)站制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行安徽網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!1、單個接口測試
#encoding=utf-8
import requests
import json
data = {'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-8
import requests
import json
import re
pattern = 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、使用隨機參數(shù)
#encoding=utf-8
import requests
import json,random
import re,string
username = [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、使用文件的的唯一數(shù)字參數(shù)使用戶唯一
#encoding=utf-8
import requests
import json,random
import re,string
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" + 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()))
5、封裝常用函數(shù)
#encoding=utf-8
import requests
import json,random
import re,string
def post_request(url,data):
res = requests.post(url,data)
return res
def 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" + username
url = '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、配置數(shù)據(jù)和程序的分離
Conf.py
url = 'http://39.106.41.11'
port = 8080
path = "register"
request_url = url + ":" + str(port) + "/" + path + "/"
#encoding=utf-8
import requests
import json,random
import re,string
from conf import *
def post_request(url,data):
res = requests.post(url,data)
return res
def 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" + username
data = {'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、配置數(shù)據(jù)、測試數(shù)據(jù)和程序的分離
Conf.py
url = 'http://39.106.41.11'
port = 8080
path = "register"
request_url = url + ":" + str(port) + "/" + path + "/"
Data.txt
{'username': username, 'password': 'wulaoshi12345', 'email': 'wulaoshi@qq.com'}|r"{'code': '00', 'userid': \d+}"
#encoding=utf-8
import requests
import json,random
import re,string
from conf import *
def post_request(url,data):
res = requests.post(url,data)
return res
def 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" + uniquenumber
with open("e:\\python\\data.txt","r") as fp:
line = fp.readline().strip()
#數(shù)據(jù)從文件讀出全部是字符串,需要獲取原類型
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)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享名稱:接口測試的幾種組織形式-創(chuàng)新互聯(lián)
標題網(wǎng)址:http://aaarwkj.com/article30/ccdopo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站維護、響應(yīng)式網(wǎng)站、建站公司、企業(yè)建站、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容