python/pytest命令行自定义参数(pytest-fixture装饰器)

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容