pytest参数关联

参数关联,也叫接口关联,即接口之间存在参数的联系或依赖。在完成某一功能业务时,有时需要按顺序请求多个接口,此时在某些接口之间可能会存在关联关系。

比如:请求登录接口后获取到token值,后续其他接口请求时需要将token作为请求参数传入。

http://123.57.39.153:5005

登录:
URL:http://test.bylemon.cn/auth
请求方式:post
请求头:Content-Type:application/json
请求体:
{"email": "Admin123@xx.com",
"pass": "Admin123"}
响应:
{'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
请求方式:GET
请求头:Authorization:Bearer +token

登录请求后有一个token值,在后面请求中需要使用token的值

使用pytest在进行关联该如何处理呢?如果按照以下处理直接存储到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())

结果:


image.png

以上可见是不可以直接使用的。那么我们该如何处理呢?

处理方式一:

将获取登录的请求方式定义一个方法,之后再setup中调用使用。

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())

处理方式二:

代码流水式调用

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")
        # 在请求中使用
        res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+token})
        print(res.json())

处理方式三:

将请求数据存储到环境变量中。

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

    # 当前用例参数为fixture装饰修饰的方法名
    def test02_get_info(self):
        # 在请求中使用
        print(config.token)
        res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+config.token})
        print(res.json())

处理方式四:

使用Fixture函数

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

    # 当前用例参数为fixture装饰修饰的方法名
    def test_get_info(self,test_login):
        # 在请求中使用
        res = requests.get(url="http://test.bylemon.cn/get/data?type=category",headers={"Authorization":"Bearer "+test_login})
        print(res.json())

这里被fixture修饰的装饰器函数不会被作为测试函数执行

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容