接口測(cè)試
import requests
import pytest
import sys
import json
sys.path.append(r"D:360MoveDataUserslenovoDesktopstartProjectOAS.Cloud.PAAS_Interface")
from common import custRandom, readCase, trafficPermit, processData, readConfig
class Test_UM_register(object):
def readData(self):
self.path = readCase.readCasedata("UM", "Test_01").getPath()
self.headers = readCase.readCasedata("UM", "Test_01").getHeaders()
self.data = readCase.readCasedata("UM", "Test_01").getData()
self.url = readConfig.r_conf('common', 'url') + self.path
return [self.url, self.headers, self.data]
@pytest.mark.parametrize(
("number", "loginName", "email", "mobile"),[
# 正確填寫(xiě)
["case1",custRandom.mix_letters(8), custRandom.mix_letters(8)+"@qq.com", "136" + custRandom.digits(8)],
# 用戶名必填
["case2", "", custRandom.mix_letters(8) + "@qq.com", "136" + custRandom.digits(8)],
# 郵箱必填
["case3",custRandom.mix_letters(8), "", "136" + custRandom.digits(8)],
# 手機(jī)號(hào)必填
["case4", custRandom.mix_letters(8), custRandom.mix_letters(8)+"@qq.com", ""],
])
def test_01(self, number, loginName, email, mobile):
# 數(shù)據(jù)獲取
dataList = self.readData()
url = dataList[0]
headers = eval(dataList[1])
data = dataList[2]
# 將用例數(shù)據(jù)導(dǎo)入
num = number
data = processData.replaceData("yeqinfang106", loginName, data)
data = processData.replaceData("840332374@qq.com", email, data)
data = processData.replaceData("13632986452", mobile, data)
data = json.loads(data) # 格式化:json.dumps()將字典轉(zhuǎn)化為字符串,json.loads()將字符串轉(zhuǎn)化為字典
# 會(huì)話保持
session = requests.session()
# 發(fā)送請(qǐng)求
response = session.post(url=url, headers=headers, data=json.dumps(data)) # data參數(shù)要求為數(shù)據(jù)格式
print(response.text)
# 校驗(yàn)數(shù)據(jù)
if num == "case1":
assert json.loads(response.text)['status'] == 1
assert json.loads(response.text)['code'] == 200
assert json.loads(response.text)['message'] == "請(qǐng)求成功"
elif num == "case2":
assert json.loads(response.text)['status'] == 0
assert json.loads(response.text)['code'] == 434
assert json.loads(response.text)['message'] == "賬號(hào)參數(shù)長(zhǎng)度過(guò)短"
elif num == "case3":
assert json.loads(response.text)['status'] == 0
assert json.loads(response.text)['code'] == 434
assert json.loads(response.text)['message'] == "郵箱地址參數(shù)長(zhǎng)度過(guò)短"
elif num == "case4":
assert json.loads(response.text)['status'] == 0
assert json.loads(response.text)['code'] == 434
assert json.loads(response.text)['message'] == "聯(lián)系電話參數(shù)長(zhǎng)度過(guò)短"
else:
pass
if __name__ =="__main__":
path = readConfig.r_conf('reportPath', 'path')
pytest.main([path, "--self-contained-html" , 'Test_01_register.py'])
這里是接口測(cè)試代碼,里面的模塊都是原先幾篇的綜合運(yùn)用。接口測(cè)試框架主要是理解他的框架思想。上面的main函數(shù)里,使用pytest運(yùn)行,接著生成測(cè)試報(bào)告。這是pytest自帶的組件,非常方便。
readData函數(shù)中,讀取的是測(cè)試用例的標(biāo)準(zhǔn)數(shù)據(jù),即可根據(jù)這些數(shù)據(jù)來(lái)設(shè)計(jì)用例數(shù)據(jù)。
test_01函數(shù)中,使用的是參數(shù)化。即通過(guò)數(shù)據(jù)處理模塊,設(shè)計(jì)需要傳入的參數(shù),這里的@pytest.mark.parametrize就是將參數(shù)不斷傳入的。這樣就可以減少大量代碼。
assert是python自帶的判斷語(yǔ)句,按正常使用即可。
上面就是簡(jiǎn)單的接口測(cè)試?yán)?,讀者可借鑒,但整個(gè)遷移的話,如果是新手,不好處理。不過(guò)可以私信我,一起學(xué)習(xí)交流。