python/pytest命令行自定义参数(pytest-fix

2021-08-03  本文已影响0人  jinjin1009

1、python命令行接收参数
1.1 脚本编写

from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('config_env')
env = sys.argv[1]
if env == 'gray':
    host = cfg.get('env', 'gray')
if env == 'release':
    host = cfg.get('env', 'release')

1.2 配置文件
其中config_env文件内容,其中注意[env]字段是在cfg.get时使用

[env]
gray: http://gray.***.com
release: http://online.***.com
release-bak: http://online-bak.***.com

1.3 命令行输入为

python3 test.py gray

2、pytest命令行自定义参数
2.1 定义conftest.py文件
生效范围,放在工程根目录会起到全局作用,在不同的测试子目录也可以放conftest.py,作用范围只在该层级以及以下目录生效
文件内容

import pytest
from configparser import ConfigParser
def pytest_addoption(parser):
    """
    增加参数 env
    """
    parser.addoption("--env", action="store", default="deeptables",
                     help="one of: deeptables, gbm")

@pytest.fixture(scope="session")
def get_host(pytestconfig):
    """
    调用该函数返回对应的host
    """
    env = pytestconfig.getoption('--env')
    return env

2.2 测试用例变更

#其中get_host是conftest.py 文件中的一个方法
def test_search_corporate(self, get_host):
        url = get_host + uri + query

2.3 命令行输入

pytest -s test.py --env=gray
上一篇下一篇

猜你喜欢

热点阅读