新建乙個需要測試的介面類 http_request.py 檔案
首先需要匯入 requests庫
函式引數存放測試資料傳入的引數,有url,data,請求方式及cookie
如果請求方式是get則進行呼叫requests庫中的get方法,如果是post則呼叫post方法,反之不支援
print("暫時不支援此請求方式")
return res建乙個測試用例類 test_http.py 檔案
匯入unittest庫,繼承其中的testcase方法,testsuit方法等
class testhttp(unittest.testcase):#建立類,繼承unittest的testcase的方法
def setup(self):#執行用例前首先執行此函式下的內容與teardown函式對應
pass
def __init__(self,methodname,url,data,method,expected):
super(testhttp,self).__init__(methodname)#超繼承,既繼承了父類的初始化函式,又不影響重寫
self.url=url #重寫的url,以下相似
self.data=data
self.method=method
self.expected=expected
def test_api(self): #介面測試用例
res=httprequest().http_request(self.url,self.data,self.method,getattr(getdata,'cookie'))#呼叫匯入的需要測試的界麵包,呼叫其中的函式,包含傳參,引數需要self,cookie是反射的用法,需要在新建乙個類名為getdata的方法,設定cookie欄位為none
if res.cookies:#如果存在cookie,就位true,執行以下語句
setattr(getdata,'cookie',res.cookies)#反射,將cookie的值賦值在getdata中的cookie
try:#捕獲異常
self.assertequal(self.expected,res.json()['status'])#self.expected為測試資料中的資料,與json返回的status欄位做比較,如果一致,測試通過
except assertionerror as e:
print("test_api 's 錯誤是:".format(e))
raise e#釋放異常執行用例以及存放測試資料的方法 test_suit.py 檔案
'''測試資料,千萬注意大括號的位置'''
test_data=[,
"expected":1,
"method":"post"},
,"expected":1,
"method":"get"}]
suite=unittest.testsuite() #建立乙個容器
for item in test_data: #迴圈執行以下載入測試用例的方法
suite.addtest(testhttp("test_api",item['url'],item['data'],item['method'],item['expected']))# test_api 是測試用例的函式名
with open("testsummer.html","wb") as file:#建立檔案,不要忘了用wb方式建立
runner=htmltestrunner.htmltestrunner(stream=file, verbosity=2, title="testsummerreport", description=none)#檔案的具體展示
runner.run(suite)#執行用例
python介面測試指令碼 執行緒 引數化
測試資料存放檔案路徑和檔案 peopleinfo c users ewang desktop infor.csv url c users ewang desktop urlinfo.csv 建立檔案控制代碼並賦予該檔案讀許可權 userinfo file peopleinfo,rb urlfile ...
python介面自動化7 引數關聯
前言 我們用自動化發帖之後,要想接著對這篇帖子操作,那就需要用引數關聯了,發帖之後會有乙個帖子的id,獲取到這個id,繼續操作傳這個帖子id就可以了 的登入機制已經變了,不能用賬號和密碼登入了,換個 或者用cookie登入吧 一 刪除草稿箱 1.我們前面講過登入後儲存草稿箱,那可以繼續接著操作 刪除...
python介面自動化7 引數關聯
前言 我們用自動化發帖之後,要想接著對這篇帖子操作,那就需要用引數關聯了,發帖之後會有乙個帖子的id,獲取到這個id,繼續操作傳這個帖子id就可以了 的登入機制已經變了,不能用賬號和密碼登入了,換個 或者用cookie登入吧 一 刪除草稿箱 1.我們前面講過登入後儲存草稿箱,那可以繼續接著操作 刪除...