參數(shù)關(guān)聯(lián)拓提,也叫接口關(guān)聯(lián)愿棋,即接口之間存在參數(shù)的聯(lián)系或依賴纯命。在完成某一功能業(yè)務(wù)時(shí)免胃,有時(shí)需要按順序請(qǐng)求多個(gè)接口,此時(shí)在某些接口之間可能會(huì)存在關(guān)聯(lián)關(guān)系辣恋。
比如:請(qǐng)求登錄接口后獲取到token值第煮,后續(xù)其他接口請(qǐng)求時(shí)需要將token作為請(qǐng)求參數(shù)傳入。
登錄:
URL:http://test.bylemon.cn/auth
請(qǐng)求方式:post
請(qǐng)求頭:Content-Type:application/json
請(qǐng)求體:
{"email": "Admin123@xx.com",
"pass": "Admin123"}
響應(yīng):
{'code': 201, 'message': '登陸成功抑党!', 'data': {'token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTE3MDc3OTQsInVzZXJfZW1haWwiOiJBZG1pbjEyM0B4eC5jb20iLCJyZWZyZXNoIjpmYWxzZX0.MSSsyRnpRBPNkWjEw_HWaNNknA4vuQW0yl30m7GsYaY', 'refresh_token': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTE4NzMzOTQsInVzZXJfZW1haWwiOiJBZG1pbjEyM0B4eC5jb20iLCJyZWZyZXNoIjp0cnVlfQ.tRIFFAr1Z56cympOgiWHyON_EfeoeHMp-2rg4diSZc0'}}
獲取信息:
URL:http://test.bylemon.cn/get/data?type=category
請(qǐng)求方式:GET
請(qǐng)求頭:Authorization:Bearer +token
登錄請(qǐng)求后有一個(gè)token值,在后面請(qǐng)求中需要使用token的值
使用pytest在進(jìn)行關(guān)聯(lián)該如何處理呢撵摆?如果按照以下處理直接存儲(chǔ)到self中是否可以呢底靠?
import requests
class TestDemo():
def test_login(self):
res = requests.post(url="http://test.bylemon.cn/auth",json={"email": "Admin123@xx.com","pass": "Admin123"})
self.token = res.json().get("data").get("token")
print(res.json())
def test_get_info(self):
res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+self.token})
print(res.json())
結(jié)果:
image.png
以上可見是不可以直接使用的。那么我們?cè)撊绾翁幚砟兀?/p>
處理方式一:
將獲取登錄的請(qǐng)求方式定義一個(gè)方法特铝,之后再setup中調(diào)用使用暑中。
import requests
def login():
res = requests.post(url="http://test.bylemon.cn/auth", json={"email": "Admin123@xx.com", "pass": "Admin123"})
token = res.json().get("data").get("token")
return token
class TestDemo():
def setup_method(self):
self.token = login()
def test_get_info(self):
res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+self.token})
print(res.json())
處理方式二:
代碼流水式調(diào)用
import requests
class TestDemo():
def test_get_info(self):
res = requests.post(url="http://test.bylemon.cn/auth", json={"email": "Admin123@xx.com", "pass": "Admin123"})
# 獲取token
token = res.json().get("data").get("token")
# 在請(qǐng)求中使用
res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+token})
print(res.json())
處理方式三:
將請(qǐng)求數(shù)據(jù)存儲(chǔ)到環(huán)境變量中壹瘟。
config文件
token = None
代碼:
import requests
import config
class TestDemo():
def test01_token(self):
res = requests.post(url="http://test.bylemon.cn/auth", json={"email": "Admin123@xx.com", "pass": "Admin123"})
token = res.json().get("data").get("token")
print(config.token)
config.token = token
# 當(dāng)前用例參數(shù)為fixture裝飾修飾的方法名
def test02_get_info(self):
# 在請(qǐng)求中使用
print(config.token)
res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+config.token})
print(res.json())
處理方式四:
使用Fixture
函數(shù)
import requests
import pytest
class TestDemo():
@pytest.fixture()
def test_login(self):
res = requests.post(url="http://test.bylemon.cn/auth", json={"email": "Admin123@xx.com", "pass": "Admin123"})
# 使用yield
token = res.json().get("data").get("token")
yield token
# 當(dāng)前用例參數(shù)為fixture裝飾修飾的方法名
def test_get_info(self,test_login):
# 在請(qǐng)求中使用
res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+test_login})
print(res.json())
這里被fixture修飾的裝飾器函數(shù)不會(huì)被作為測(cè)試函數(shù)執(zhí)行