pycharm中使用request和Pytest進行接口測試的方法
安裝request庫
以火車的站站查詢?yōu)槔膒ost和get方法的接口測試
使用pytest測試接口
1、requests的請求機制
1、安裝request庫
2、以火車的站站查詢?yōu)槔膒ost和get請求方法
2.1get請求:
兩種傳參方式
1、_url = “網(wǎng)址+參數(shù)” = “網(wǎng)址?key1=value1&key2=value2”
response1 = request.get(url = _url)
2、字典拼接
_params = { “key1” : “value1”, “key2” : “value2”, } response2 = requests.get(url=“網(wǎng)址”, params = _params)
import requests response = requests.get(url="https://api.binstd.com/train/station2s?start=北京&end=西安&ishigh=0&appkey=d737aad9a0d9dc97") print(response.text) #字符串格式 print(response.json()) #json,前提需要確保返回內(nèi)容為json格式,否則報錯 #字典方式拼接參數(shù) print("-------------字典方式拼接參數(shù)---------------") params = { "start" : "北京", "end" : "西安", "ishigh" : 0 , "appkey" : "d737aad9a0d9dc97" } response1 = requests.get(url="https://api.binstd.com/train/station2s", params = params) print(response1.text) print(response1.json())
2.2post請求
拼接參數(shù)方式傳參
import requests #字典方式拼接參數(shù) data = { "start" : "北京", "end" : "西安", "ishigh" : 0 , "appkey" : "d737aad9a0d9dc97" } response1 = requests.post(url="https://api.binstd.com/train/station2s", data = data) print(response1.text) print(response1.json()) #獲取響應(yīng)狀態(tài)碼 print(response1.status_code) #獲取原始模式 print(response1.raw)
常見的請求方法
請求方法 | 含義 |
---|---|
requests.get() | 獲取html的主要方法 |
requests.head() | 獲取html頭部信息的主要方法 |
requests.post() | 向html網(wǎng)頁提交post請求的方法 |
requests.put() | 向html網(wǎng)頁提交put請求的方法 |
requests.patch() | 向html提交局部修改的請求 |
requests.delete() | 向html提交刪除請求 |
2、pytest測試接口
1、安裝pytest
pip install pytest
2、使用pytest測試接口
在pytest框架中,有如下約束:
文件名要以test開頭或者結(jié)尾(test_*.py / *_test.py),可以包含一個或多個test_開頭的函數(shù)。
此時,在執(zhí)行pytest命令時,會自動從當前目錄及子目錄中尋找符合上述約束的測試函數(shù)來執(zhí)行。
4.1首先得到響應(yīng)數(shù)據(jù)
import requests def request_ticket(): #返回接口響應(yīng)結(jié)果 url = "https://api.binstd.com/train/ticket" payload = { "start": "北京", "end": "西安", "date": "2019-10-1", "appkey": "d737aad9a0d9dc97" } #response = requests.get(url = _url, parms = payload) response = requests.post(url = url, data = payload) print(response.text) return response request_ticket()
4.2為了方便查看將響應(yīng)結(jié)果格式化:由于太長,部分用省略號代替
{ "status": 0, "msg": "ok", "result": { "start": "北京", "end": "西安", "date": "2020-06-10", "list": [ { "trainno": "G667", "type": "G", "typename": "高鐵", "station": "北京西", "endstation": "西安北", "departuretime": "11:19", ... "departstationcode": "BXP", "terminalstationcode": "EAY", "startdate": "20200610", ... }, { "trainno": "G659", "type": "G", "typename": "高鐵", "station": "北京西", "endstation": "西安北", "departuretime": "11:53", ... "departstationcode": "BXP", "terminalstationcode": "EAY", "startdate": "20200610", ... }, {...}, {...}, ... ] } }
4.3取出數(shù)據(jù)
出發(fā)站(station)和到達站(endstation)在result中的list下,怎么取到呢?----[“result”] [“l(fā)ist”]
---- request_ticket().json()[“result”][“l(fā)ist”]
def test_departur_station(): """ 始發(fā)站測試,測試接口返回的所有車次信息,他們的出發(fā)站,和到達站都符合參數(shù)約定 :return: """ #從響應(yīng)中獲取測試列表 trainSli = request_ticket().json()["result"]["list"] #單個的車次信息 #trainSli是取出來的list列表 for trainInfo in trainSli: assert "北京" in trainInfo["station"] #判斷‘北京'是否是列表中‘station'的值 assert "西安" in trainInfo["endstation"] #判斷到達站是不是‘西安' #調(diào)用函數(shù) test_departur_station() '''def test_train_date(): """ 發(fā)車日期測試,接口返回的所有車次信息,發(fā)車日期,都符合參數(shù)約定 :return: """ #從響應(yīng)中獲取測試列表 trainSli = request_ticket().json()["result"]["list"] #單個的車次信息 for trainInfo in trainSli: assert "20200610" in trainInfo["startdate"] test_train_date()'''
4.4 運行
4.5 查看結(jié)果
如果該路徑下有多個以test開頭或者結(jié)尾的文件,則會一起檢測兩個文件中的接口
如果出現(xiàn)ERROR則在文件中找錯誤原因
總結(jié)
到此這篇關(guān)于pycharm中使用request和Pytest進行接口測試的文章就介紹到這了,更多相關(guān)pycharm使用request和Pytest接口測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python進行數(shù)據(jù)科學工作的簡單入門教程
這篇文章主要介紹了Python進行數(shù)據(jù)科學工作的簡單入門教程,主要針對Python發(fā)行版Anaconda進行說明,需要的朋友可以參考下2015-04-04Pandas數(shù)據(jù)清洗和預處理的實現(xiàn)示例
本文主要介紹了Pandas數(shù)據(jù)清洗和預處理的實現(xiàn)示例,包括處理缺失值、異常值,進行數(shù)據(jù)轉(zhuǎn)換和規(guī)范化,以及處理重復數(shù)據(jù)等操作,感興趣的可以了解一下2024-01-01python 利用matplotlib在3D空間繪制二次拋物面的案例
這篇文章主要介紹了python 利用matplotlib在3D空間繪制二次拋物面的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02pytorch通過miniconda安裝的實現(xiàn)示例
本文主要介紹了pytorch通過miniconda安裝的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-06-06Python中使用defaultdict和Counter的方法
本文深入探討了Python中的兩個強大工具——defaultdict和Counter,并詳細介紹了它們的工作原理、應(yīng)用場景以及在實際編程中的高效使用方法,感興趣的朋友跟隨小編一起看看吧2025-01-01