pytest參數(shù)關(guān)聯(lián)

參數(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ù)傳入。

http://123.57.39.153:5005

登錄:
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())

適用于當(dāng)前類中使用

處理方式二:

代碼流水式調(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())

適用于跨文件使用

處理方式四:
使用類屬性

import requests


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)
        TestDemo.token = token

    # 當(dāng)前用例參數(shù)為fixture裝飾修飾的方法名
    def test02_get_info(self):
        # 在請(qǐng)求中使用
        res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+TestDemo.token})
        print(res.json())

適用于當(dāng)前類中使用

處理方式五:

使用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í)行
屬于前置處理,比較負(fù)載條件下使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容