增加缓存使用方式
This commit is contained in:
parent
fffc80efc2
commit
7f27093a2d
|
@ -4,62 +4,21 @@
|
||||||
# @Author : wangjie
|
# @Author : wangjie
|
||||||
# @File : conftest.py
|
# @File : conftest.py
|
||||||
# @project : SensoroApi
|
# @project : SensoroApi
|
||||||
import allure
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from common.base_api import BaseApi
|
from common.base_api import BaseApi
|
||||||
from common.base_log import logger
|
from common.base_log import logger
|
||||||
from common.exceptions import ValueNotFoundError
|
|
||||||
from pageApi.login import Login
|
from pageApi.login import Login
|
||||||
from utils.allure_handle import allure_attach_text
|
from utils.cache_handle import CacheHandler
|
||||||
|
|
||||||
# 定义一个全局变量,用于存储提取的参数内容
|
|
||||||
_global_data = {}
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session", autouse=False)
|
@pytest.fixture(scope="session", autouse=True)
|
||||||
def set_global_data():
|
|
||||||
"""
|
|
||||||
设置全局变量,用于关联参数
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
|
|
||||||
def _set_global_data(cache_name, value):
|
|
||||||
_global_data[cache_name] = value
|
|
||||||
with allure.step("提取"):
|
|
||||||
allure_attach_text("设置变量", str(f"'{cache_name}':'{value}'"))
|
|
||||||
allure_attach_text("当前可使用的全局变量", str(_global_data))
|
|
||||||
|
|
||||||
yield _set_global_data
|
|
||||||
_global_data.clear()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session", autouse=False)
|
|
||||||
def get_global_data():
|
|
||||||
"""
|
|
||||||
从全局变量global_data中取值
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
|
|
||||||
def _get_global_data(cache_data):
|
|
||||||
try:
|
|
||||||
with allure.step("提取"):
|
|
||||||
allure_attach_text("取出来的变量", str(f"{cache_data}:{_global_data.get(cache_data, None)}"))
|
|
||||||
return _global_data[cache_data]
|
|
||||||
except KeyError:
|
|
||||||
with allure.step("获取变量失败,当前可使用的全局变量"):
|
|
||||||
allure_attach_text("获取变量失败,当前可使用的全局变量", str(_global_data))
|
|
||||||
raise ValueNotFoundError(f"{cache_data}的缓存数据未找到,请检查是否将该数据存入缓存中")
|
|
||||||
|
|
||||||
return _get_global_data
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session", autouse=False)
|
|
||||||
def get_token():
|
def get_token():
|
||||||
"""获取登录V1的token"""
|
"""获取登录V1的token"""
|
||||||
logger.info("开始用例前置操作")
|
logger.info("开始用例前置操作")
|
||||||
# 调登录接口,获取登录接口的token¬
|
# 调登录接口,获取登录接口的token¬
|
||||||
login_response = Login().login('18800000001', '123456')
|
login_response = Login().login('18800000001', '123456')
|
||||||
token = BaseApi.get_json(login_response)['data']['token']
|
token = BaseApi.get_json(login_response)['data']['token']
|
||||||
|
CacheHandler.set_cache('Authorization', f'Bearer {token}')
|
||||||
logger.info("结束用例前置操作")
|
logger.info("结束用例前置操作")
|
||||||
return token
|
return token
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
# !/usr/bin/python
|
||||||
|
# -*- coding:utf-8 -*-
|
||||||
|
# @Time : 2023/9/27 20:57
|
||||||
|
# @Author : wangjie
|
||||||
|
# @File : cache_handle.py
|
||||||
|
# @project : SensoroApiAutoTest
|
||||||
|
import json
|
||||||
|
|
||||||
|
import allure
|
||||||
|
|
||||||
|
from common.exceptions import ValueNotFoundError
|
||||||
|
from utils.allure_handle import allure_attach_text, allure_attach_json
|
||||||
|
|
||||||
|
# 定义一个全局变量,用于存储提取的参数内容
|
||||||
|
_global_data = {}
|
||||||
|
|
||||||
|
|
||||||
|
class CacheHandler:
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def set_cache(cache_name, value):
|
||||||
|
"""
|
||||||
|
往全局变量_global_data中存值,用于关联参数
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
|
||||||
|
_global_data[cache_name] = value
|
||||||
|
with allure.step("设置缓存成功"):
|
||||||
|
allure_attach_text("存入缓存", str(f"'{cache_name}':'{value}'"))
|
||||||
|
allure_attach_json("当前可使用的缓存", json.dumps(_global_data, ensure_ascii=False, indent=4))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_cache(cache_data):
|
||||||
|
"""
|
||||||
|
从全局变量_global_data中取值
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
with allure.step("提取缓存成功"):
|
||||||
|
allure_attach_text("取出缓存", str(f"{cache_data}:{_global_data.get(cache_data, None)}"))
|
||||||
|
return _global_data[cache_data]
|
||||||
|
except KeyError:
|
||||||
|
with allure.step("提取缓存失败"):
|
||||||
|
allure_attach_json("提取缓存失败,当前可使用的缓存",
|
||||||
|
json.dumps(_global_data, ensure_ascii=False, indent=4))
|
||||||
|
raise ValueNotFoundError(f"{cache_data}的缓存数据未找到,请检查是否将该数据存入缓存中")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
CacheHandler.set_cache("a", "1")
|
||||||
|
print(CacheHandler.get_cache("a"))
|
Loading…
Reference in New Issue