###代码部分
import os
from typing import Union
BASE_DIR: Union[bytes, str] = os.path.dirname(os.path.abspath(__file__))
# 添加一个命令行参数
# 你可以在Terminal命令行中输入pyetst --help |group ZR可查看
def pytest_addoption(parser):
mygroup = parser.getgroup('ZR')
mygroup.addoption('--test', # 注册一个命令选项
default='test', # 参数默认值
dest='env',
help='set your run env' # 帮助提示 参数的详细描述
)
@pytest.fixture(scope="session")
def cmdoption(request):
env = request.config.getoption('--env', default='test')
if env == 'dev' or env == 'DEV':
datapath = BASE_DIR + r"/TestData/datas/dev/datas.yaml"
# print(my_file_content(datapath))
elif env == 'test' or env == 'TEST':
datapath = BASE_DIR + r"/TestData/datas/test/datas.yaml"
# print(my_file_content(datapath))
elif env == 'pre' or env == 'PRE':
datapath = BASE_DIR + r"/TestData/datas/pre/datas.yaml"
# print(my_file_content(datapath))
else:
raise EnvironmentError(f'Please set up your environment ! Current Context:{env}')
with open(datapath) as f:
datas = yaml.safe_load(f)
return env, datas
问题
报错信息
在Terminal命令行中输入pytest --env DEV get_api_url.py::get_app_url -vs 进行环境切换 报错
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0x80 in position 156: illegal multibyte sequence
我通过python的chardet模块写后执行 还是无法读取
import chardet
def my_file_content(file_path):
"""
使用了 `chardet` 模块来自动检测文件的编码方式,然后使用检测到的编码方式对文件进行解码。
:param file_path: 你的文件路径
:return:
"""
with open(file_path, 'rb') as f:
data = f.read()
file_encoding = chardet.detect(data)['encoding']
file_content = data.decode(file_encoding)
return file_content
‘’‘’
报错信息
Failed: Fixture "cmdoption" called directly. Fixtures are not meant to be called directly,
### 环境
yaml文件
‘’‘yaml
DEV:
app:
host: http://192.168.1.181
port:8888
'''