Ai
1 Star 0 Fork 0

乐瓜乐虫/python_api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
conftest.py 1.69 KB
一键复制 编辑 原始数据 按行查看 历史
"""
定义登录夹具
pytest的fixture的定义
1、def 定义一个普通
2、申明夹具: @pytest.fixtrue
3、做登录操作 目的 : 得到token
- jsonpath 提取token
4、设置夹具返回值: yield token
"""
import pytest
import requests
from jsonpath import jsonpath
from loguru import logger
from data.setting import base_url
@pytest.fixture()
def login_fixture(get_env): #调用夹具
logger.info("--------------前置代码处理-------------")
param = {"principal": "lemon_py", "credentials": "12345678", "appType": 3, "loginType": 0}
url = f'{base_url[get_env]}/login' # 通过夹具的返回值 拼接地址
res = requests.request("post", url, json=param)
access_token = jsonpath(res.json(), '$..access_token')[0]
token_type = jsonpath(res.json(), '$..token_type')[0] # bearer
token = token_type + access_token # 拼接登录之后返回的token 给后面的接口鉴权
logger.info(f"夹具返回的token是{token}")
yield token # 夹具的返回值 token 调用夹具得到这个返回值
# 添加钩子函数: 注册pytest的参数的 用于传递测试环境名字 【钩子函数不能改】
def pytest_addoption(parser):
# 注册自定义参数命令行参数
parser.addoption("--env", default="test", choices=['dev', 'test', 'uat', 'pro'], help="命令行参数 '--env' 设置环境切换")
# 定义一个夹具获取--env参数的值
@pytest.fixture()
def get_env(request): # 【夹具的名字可以改】
# 从命令行参数中读取env参数的值 --dev pro uat test 等,字典的key
option_env = request.config.getoption("--env")
print(f"--env的参数值是:{option_env}")
yield option_env # 返回这个参数的值
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/andyliuyun/python_api.git
git@gitee.com:andyliuyun/python_api.git
andyliuyun
python_api
python_api
master

搜索帮助