基於requests框架實現介面自動化測試專案實戰

每天進步一點點,關注我們哦,每天分享測試技術文章

本文章出自【碼同學軟體測試】

碼同學公眾號:自動化軟體測試

碼同學抖音號:小碼哥聊軟體測試

requests庫是一個常用的用於http請求的模組,它使用python語言編寫,在當下python系列的介面自動化中應用廣泛,本文將帶領大家深入學習這個庫

Python環境的安裝就不在這裡贅述了,我們直接開幹。

01

requests的安裝

windows下執行如下命令

pipinstallrequests-ihttp://pypi。douban。com/simple/——trust-host pypi。douban。com

mac終端下執行如下命令

python3-mpipinstallrequests-ihttp://pypi。douban。com/simple/——trust-host pypi。douban。com

02

自動化requests模組的常用方法

基於requests框架實現介面自動化測試專案實戰

2.1get請求實戰

# !/usr/bin python3 # encoding: utf-8 -*- # @author: 沙陌 微信:Matongxue_2# @Time:2021/3/25 9:54 # @Copyright:北京碼同學網路科技有限公司import requests host=‘http://10。0。0。18:8080’def get(): “”“get介面請求:return:”“” url =host+‘/pinter/com/getSku’ #介面地址 params={ ‘id’:1} resp = requests。get(url,params=params) status_code=resp。status_code #獲取響應狀態碼 print(‘響應狀態碼:{}’。format(status_code)) text=resp。text #獲取響應內容,結果型別是字串 print(‘響應內容:{}’。format(text)) json=resp。json() #獲取響應內容,結果是字典型別 print(‘響應內容:{}’。format(json)) resp_headers=resp。headers #獲取響應headers print(‘響應header:{}’。format(resp_headers))if__name__==‘__main__’: get()

結果如下

D:\Python\Python36\python。exe D:/pycharmprojects/first/requetsstudy/pinter。py響應狀態碼:200 響應內容:{“code”:“0”,“message”:“success”,“data”:{“skuId”:1,“skuName”:“ptest-1”,“price”:“645”,“stock”:709,“brand”:“testfan”}}響應內容:{‘code’:‘0’,‘message’:‘success’,‘data’:{‘skuId’:1,‘skuName’:‘ptest-1’,‘price’:‘645’,‘stock’:709,‘brand’:‘testfan’}}響應header:{‘Content-Type’:‘application/json;charset=UTF-8’,‘Transfer-Encoding’:‘chunked’,‘Date’:‘Fri,12Mar202122:13:49GMT’,‘Keep-Alive’:‘timeout=20’,‘Connection’:‘keep-alive’}Process finished with exit code 0

上述程式碼中請求發起後得到一個響應物件變數resp,那麼resp物件的常用方法如下

基於requests框架實現介面自動化測試專案實戰

2.2post請求實戰

post請求的引數格式通常有多種,我們依次學習

第一種表單形式的引數

```python

importrequests

host=

‘http://10。0。0。18:8080’

defpost():“”“post表單:return:”“”url=host+‘/pinter/com/login’#表單引數data={

‘userName’:‘沙陌’,‘password’:‘123456’}resp=requests。post(url=url,data=data)status_code=

resp。status_code#獲取響應狀態碼print(‘響應狀態碼:{}’。format(status_code))text=resp。text#獲取響應內容,結果型別是字串print(‘響應內容:{}’。format(text))json=resp。json()#獲取響應內容,結果是字典型別print(‘響應內容:{}’。format(json))resp_headers=resp。headers#獲取響應headers

print(‘響應header:{}’。format(resp_headers))```

第二種json格式引數

import requestshost=‘http://10。0。0。18:8080’def post_json(): “”“postjson:return:”“” url =host +‘/pinter/com/register’ #header裡定義引數型別 headers={ ‘Content-Type’:‘application/json’} #json引數 json={ “userName”:“沙陌”, “password”:“1234”, “gender”:1, “phoneNum”:“110”, “email”:“beihe@163。com”, “address”:“Beijing”} resp=requests。post(url=url,json=json) status_code=resp。status_code #獲取響應狀態碼 print(‘響應狀態碼:{}’。format(status_code)) text=resp。text #獲取響應內容,結果型別是字串 print(‘響應內容:{}’。format(text)) json=resp。json() #獲取響應內容,結果是字典型別 print(‘響應內容:{}’。format(json)) resp_headers=resp。headers #獲取響應headers print(‘響應header:{}’。format(resp_headers))

2.3put介面實戰

import requestshost=‘http://10。0。0。18:8080’def put(): “”“put 清酒:return:”“” url = host+‘/pinter/com/phone’ #介面地址 #引數 json={ “brand”:“Huawei”, “color”:“yellow”, “memorySize”:“64G”, “cpuCore”:“8核”, “price”:“8848”, “desc”:“全新上市”} resp=requests。put(url=url,json=json) status_code=resp。status_code #獲取響應狀態碼 print(‘響應狀態碼:{}’。format(status_code)) text=resp。text #獲取響應內容,結果型別是字串 print(‘響應內容:{}’。format(text)) json=resp。json() #獲取響應內容,結果是字典型別 print(‘響應內容:{}’。format(json)) resp_headers=resp。headers #獲取響應headers print(‘響應header:{}’。format(resp_headers))

2.4delete請求

基於requests框架實現介面自動化測試專案實戰

2.5request.session.request用法

可以自動管理cookie,比如如下需要採用cookie認證的介面

免費領取 碼同學軟體測試 課程筆記+超多學習資料+完整影片+最新面試題,可以

轉發文章 + 私信「碼同學666」獲取資料哦

基於requests框架實現介面自動化測試專案實戰

基於requests框架實現介面自動化測試專案實戰

結果如下:

D:\Python\Python36\python。exeD:/pycharmprojects/first/requetsstudy/pinter。py 響應狀態碼:200 響應內容:{“code”:“0”,“message”:“success”,“data”:“$22,378,198”}Process finished with exit code 0

2.6token關聯的介面如何做呢?

基於requests框架實現介面自動化測試專案實戰

對於需要token關聯的介面來說,需要從登入介面的返回值中提取token資訊,並傳遞給需要token的介面

基於requests框架實現介面自動化測試專案實戰

結果如下:

D:\Python\Python36\python。exeD:/pycharmprojects/first/requetsstudy/pinter1。py響應狀態碼:200 響應內容:{“code”:“0”,“message”:“success”,“data”:“$74,780,457”}Process finished with exit code 0

總結一下:

requests庫的請求方法裡引數眾多,所以簡單劃分一下,查詢引數就用params=params

表單引數就用data=data

json引數就用json=json

請求頭資訊header就用headers=headers

免費領取碼同學軟體測試課程筆記+超多學習資料+學習完整影片,可以關注我們公眾號哦:自動化軟體測試

本文著作權歸作者所有,任何形式的轉載都請聯絡作者獲得授權並註明出處。