您好,登錄后才能下訂單哦!
requests是python的接口測試的類庫。
一、安裝
直接使用pip install requests即可安裝。
二、發送一個get請求
response的返回包含了body、cookies、status、headers等信息,在拿到response之后,可以通過text,content等方法,得到body等的信息,可以通過cookies、encoding、status_code等屬性獲取關鍵信息。從而使用這些返回值進行斷言判斷。
# coding:utf-8 import requests import json import unittest.test.test_assertions class TestV3(unittest.TestCase): def test_get(self): host = "https://ruby-china.org" path = "/api/v3/topics?limit=1" url = host + path headers = {"Content-Type":"Application/json","User-Agent":"Chrome"} response = requests.get(url, headers = headers) #p text = json.loads(response.text) content = json.loads(response.content) apparent_encoding = response.apparent_encoding links = response.links redirect = response.is_permanent_redirect is_redirect = response.is_redirect next = response.next ok = response.ok #f header = response.headers url = response.url status_code = response.status_code reason = response.reason request = response.request raw = response.raw encoding = response.encoding cookies = response.cookies # print('text:',text) # print('content:',content) # print('header:',header) # print('url:',url) # print('redirect:',redirect) # print('status_code:',status_code) # print('reason:',reason) # print('request:',request) # print('raw:',raw) # print('encoding:',encoding) # print('apparent_encoding:',apparent_encoding) # print('links:',links) # print('cookies:',cookies) # print('is_redirect:',is_redirect) # print('next:',next) # print('ok:',ok) self.assertEqual(status_code, 200) self.assertEqual(redirect, False) self.assertEqual(encoding,'utf-8') if __name__ == '__main__': unittest.main()
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。