import os
import time
import yaml
import pytest
# 項目目錄路徑
_project_dir= os.path.dirname(os.path.abspath(__file__))
def pytest_addoption(parser):
? ? """注冊自定義參數(shù) env 到配置對象滋戳,鉤子函數(shù)自定義命令行"""
? ? parser.addoption("--env",
? ? default="stage",
? ? choices=["stage","drp","prod"],
? ? help="將命令行參數(shù) ’--env' 添加到 pytest 配置中,區(qū)分三個環(huán)境")
@pytest.fixture(scope="session",autouse=True)
def get_env(request):
? ? """從配置對象中讀取自定義參數(shù)的值"""
? ? return request.config.getoption("--env")
@pytest.fixture(scope="session",autouse=True)
def set_env(get_env):
? ? """將自定義參數(shù)的值寫入全局配置文件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)