一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - 關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

2022-01-24 00:24人生是一場(chǎng)彩排 Python

requests是一個(gè)很實(shí)用的Python HTTP客戶端庫,Requests是Python語言的第三方的庫,專門用于發(fā)送HTTP請(qǐng)求,這篇文章主要介紹了python實(shí)現(xiàn)requests接口測(cè)試,需要的朋友可以參考下

requests接口測(cè)試的介紹

requests是一個(gè)很實(shí)用的Python HTTP客戶端庫,編寫爬蟲和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,Requests是Python語言的第三方的庫,專門用于發(fā)送HTTP請(qǐng)求

 

requests接口測(cè)試的使用前提

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

pip install requests

1.requests中的get請(qǐng)求

1 GET無參請(qǐng)求

r = requests.get('http://www.baidu.com')

案例:

import requests
class Classrequset:
  def Claete(self):
      r = requests.get('http://www.baidu.com')
      print(r.text)
a=Classrequset()
a.Claete()     

2.GET傳參

payload = {'key1': 'value1', 'key2': 'value2', 'key3': None}
r = requests.get('http://www.baidu.com ', params=payload)

案例:

  def XWTTMethod(self):
      params = {"type": "guonei", "key": "4b72107de3a197b3bafd9adacf685790"}
      r = requests.get("http://v.juhe.cn/toutiao/index", params=params)
      print(r.text)
a=Classrequset()
a.XWTTMethod()

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

2.requests中的post請(qǐng)求

payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", data=payload)

案例:

def XWTTMethodpost(self):
      uripost="http://v.juhe.cn/toutiao/index"
      datapost={"type":"youxi","page":"1","size":"10","key":"ff64bdb75dd1fbc636724101514cfbe7"}
      r =requests.post(url=uripost,data=datapost)
      print(r.text)
      # print(r.status_code) #這是查看狀態(tài)碼的
a=Classrequset()
a.XWTTMethodpost()

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

3.Requests響應(yīng)

r.status_code        響應(yīng)狀態(tài)碼
r.heards             響應(yīng)頭
r.cookies            響應(yīng)cookies
r.text               響應(yīng)文本
r. encoding          當(dāng)前編碼
r. content		    以字節(jié)形式(二進(jìn)制)返回

最常用的是根據(jù)響應(yīng)狀態(tài)碼判斷接口是否連通,經(jīng)常用于做接口中斷言判斷

4.Request擴(kuò)充

1:添加等待時(shí)間 requests.get(url,timeout=1) #超過等待時(shí)間則報(bào)錯(cuò) 2:添加請(qǐng)求頭信息 requests.get(url,headers=headers) #設(shè)置請(qǐng)求頭 3:添加文件 requests.post(url, files=files) #添加文件

文件傳輸

url = 'http://httpbin.org/post'
files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=files)

5.python實(shí)現(xiàn)requests+pytest+allure的操作

1 流程如下

讀取文件中的數(shù)據(jù) requests拿到數(shù)據(jù)請(qǐng)求接口返回狀態(tài)碼通過斷言驗(yàn)證返回狀態(tài)碼和200對(duì)比生成allure的測(cè)試報(bào)告

6.讀取csv文件流程

1 存儲(chǔ)數(shù)據(jù)(csv)

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

2 讀取數(shù)據(jù)(readDemo)

import csv
class ReadCsv():
  def readCsv(self):
      item = []
      rr = csv.reader(open("../request/1212223.csv"))
      for csv_i in rr:
          item.append(csv_i)
      return item

a=ReadCsv()
print(a.readCsv())

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

3 request請(qǐng)求接口返回狀態(tài)碼

from request.dataDemo import ReadCsv
import requests
r=ReadCsv()
ee=r.readCsv()
ltms=[]
class RequestClass:
  def requesthome(self):
      for a in ee:
          if a[2]=="get":
            ss=requests.get(url=a[0],params=a[1])
            ltms.append(ss.status_code)
          else:
            ss=requests.post(url=a[0],data=a[1])
            ltms.append(ss.status_code)
      return ltms
q=RequestClass()
print(q.requesthome())

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

4 pytest斷言設(shè)置并結(jié)合allure生成測(cè)試報(bào)告

import pytest, allure, os
from request.request03_csv import RequestClass

r = RequestClass()
aa = r.requesthome()


class TestRequest:
  def testcvsHose(self):
      for s in aa:
          assert s == 200


if __name__ == '__main__':
  pytest.main(['--alluredir','report/result','requests_test.py'])
  split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
  os.system(split)

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

5 測(cè)試報(bào)告展示

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

7.讀取excle文件流程

1 存儲(chǔ)數(shù)據(jù)(xlsx)

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

2 讀取數(shù)據(jù)(readDemo)

from openpyxl import load_workbook
class UseExcel():
  def get_TestExcel(self):
      # 打開表
      workbook = load_workbook('./777.xlsx')
      # 定位表單
      sheet = workbook['Sheet1']
      print(sheet.max_row)     #3 行
      print(sheet.max_column)  #3 列
      test_data = []#把所有行的數(shù)據(jù)放到列表中
      for i in range(2,sheet.max_row+1):
          sub_data = {}#把每行的數(shù)據(jù)放到字典中
          for j in range(1,sheet.max_column+1):
              sub_data[sheet.cell(1,j).value] = sheet.cell(i,j).value
          test_data.append(sub_data)#拼接每行單元格的數(shù)據(jù)
      return test_data
t = UseExcel()
f = t.get_TestExcel()
print(f)

3.request請(qǐng)求接口返回狀態(tài)碼

import requests
from request.requestxls import UseExcel
a=UseExcel()
f = a.get_TestExcel()
item = []
class Use_Requestexcel():
  def qualification_mord(self):
      for excel_i in f:
           if excel_i["method"] == "get":
              rr = requests.get(url=excel_i["url"],params=excel_i["paras"])
              item.append(rr.status_code)
           else:
              rr = requests.post(url=excel_i["url"],data=excel_i["paras"])
              item.append(rr.status_code)
      return item

r=Use_Requestexcel()

4 pytest斷言設(shè)置并結(jié)合allure生成測(cè)試報(bào)告

import pytest, allure, os
from request.requestextes import Use_Requestexcel
r = Use_Requestexcel()
aa = r.qualification_mord()

print(aa)
class Testrequest:
  def testcvsHose(self):
      for s in aa:
          assert s == 200

if __name__ == '__main__':
  pytest.main(['--alluredir','report/result','test_req.py'])
  split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean'
  os.system(split)

5 測(cè)試報(bào)告展示

關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題

到此這篇關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的文章就介紹到這了,更多相關(guān)python requests接口測(cè)試內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

原文鏈接:https://blog.csdn.net/m0_61430050/article/details/120705009

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩永久在线观看免费视频 | 午夜在线观看免费观看 视频 | 91精品国产美女福到在线不卡 | 日本一区二区三区在线 视频 | 国产情侣自拍网 | 国产馆| 日韩中文字幕在线不卡 | 国产va欧美va在线观看 | 四虎在线最新永久免费 | 精品国产日韩一区三区 | 99国产高清久久久久久网站 | 美女被吸乳老师羞羞漫画 | 九九精品成人免费国产片 | 亚洲AV久久无码精品蜜桃 | 俄罗斯一级淫片 | 成年看片免费高清观看 | 成年人免费观看的视频 | 国产欧美日韩不卡一区二区三区 | 果冻传媒九一制片厂网站 | 国产精品久久久久久久久ktv | 91久久碰国产 | 2021国产麻豆剧传媒剧情最新 | 福利片中文 | 国产综合久久久久 | 无人区1免费完整观看 | 幻女free性俄罗斯第一次摘花 | 冰山美人调教耻辱h | 久久精品亚洲热综合一本 | 欧美老人与小伙子性生交 | 精品在线免费播放 | 亚洲国产成人久久77 | 风间由美一区二区播放合集 | 天天干夜夜玩 | 娇妻被朋友征服中文字幕 | 男同桌扒开女同桌胸罩喝奶 | 国产亚洲精品看片在线观看 | 貂蝉沦为姓奴小说 | 97影院3| 51精品| 岛国不卡 | 91精品国产高清久久久久久 |