import os
import time
import yaml
import pytest
# 项目目录路径
_project_dir= os.path.dirname(os.path.abspath(__file__))
def pytest_addoption(parser):
"""注册自定义参数 env 到配置对象,钩子函数自定义命令行"""
parser.addoption("--env",
default="stage",
choices=["stage","drp","prod"],
help="将命令行参数 ’--env' 添加到 pytest 配置中,区分三个环境")
@pytest.fixture(scope="session",autouse=True)
def get_env(request):
"""从配置对象中读取自定义参数的值"""
return request.config.getoption("--env")
@pytest.fixture(scope="session",autouse=True)
def set_env(get_env):
"""将自定义参数的值写入全局配置文件conf.yaml,
tep框架的fixyur.py
@pytest.fixture(scope="session")
def config():
config_path = os.path.join(Project.dir, "conf.yaml")
with open(config_path, "r", encoding="utf-8") as f:
conf = yaml.load(f.read(), Loader=yaml.FullLoader)
return conf的方法,
定义了获取值的方法,为了不侵入框架修改代码,本地conftest添加该方法从命令中获取env的value"""
with open(_project_dir+ "/conf.yaml","w",encoding="utf-8") as f:
yaml.dump(dict(env=get_env), f)