小门板儿

Menu

pytest:Hook 方法之 pytest_addoption

pytest_addoption 可以让用户注册一个自定义的命令行参数,方便用户将数据传递给 pytest;

这个 Hook 方法一般和 内置 fixture pytestconfig 配合使用,pytest_addoption 注册命令行参数,pytestconfig 通过配置对象读取参数的值;

一、参数说明

name:自定义命令行参数的名字,可以是:"foo", "-foo" 或 "--foo";
action:在命令行中遇到此参数时要采取的基本操作类型;
nargs:应该使用的命令行参数的数量;
const:某些操作和nargs选择所需的常量值;
default:如果参数不在命令行中,则生成的默认值。
type:命令行参数应该转换为的类型;
choices:参数允许值的容器;
required:命令行选项是否可以省略(仅可选);
help:对参数作用的简要说明;
metavar:用法消息中参数的名称;
dest:要添加到 parse_args() 返回的对象中的属性的名称;

1、action

1.1、action="store"

默认,只存储参数的值,可以存储任何类型的值,此时 *default* 也可以是任何类型的值,而且命令行参数多次使用也只能生效一个,最后一个值覆盖之前的值;

# content of conftest.py
import pytest

def pytest_addoption(parser):
    parser.addoption(
        "--cmdopt", action="store", default="type1", help="my option: type1 or type2"
    )
    parser.addoption(
        "--env", action="store", default="dev", help="env:表示测试环境,默认dev环境"
    )


@pytest.fixture()
def cmdopt(pytestconfig):
    return pytestconfig.getoption("cmdopt")


@pytest.fixture()
def env(request):
    return request.config.getoption("--env")

#content of test_param1.py
def test_option(env):
    if env == 'dev':
        print("当前测试环境为:{},域名切换为开发环境".format(env))
    elif env == 'test':
        print("当前测试环境为:{},域名切换为测试环境".format(env))
    else:
        print("环境错误,当前环境{}不存在".format(env))
不带参数运行:
D:\pytest\learn01>pytest -s -q  e/test_param1.py
当前测试环境为:dev,域名切换为开发环境
.
1 passed in 0.01s
带参数运行:
D:\pytest\learn01>pytest -s -q  --env=test e/test_param1.py
当前测试环境为:test,域名切换为测试环境
.
1 passed in 0.01s

1.2、action=“append”

存储一个列表,用 append 模式 将可以同时多次使用自定义参数,并且 default 默认值必须是一个列表,pytest 会把 default 默认参数的值和多个自定义参数的值放在一个列表中:

#content of conftest.py
def pytest_addoption(parser):
    parser.addoption(
        "--keys", action="append", default=['default key'], help="将命令行参数 ’--cmdopt' 添加到 pytest 配置中"
    )
@pytest.fixture()
def keys(request):
    return request.config.getoption("--keys") 

# content of test_param1.py
def test_keys(keys):
    print(keys)
输出结果:
D:\pytest\learn01>pytest -s -q  --keys=test1  e/test_param1.py
['default key', 'test1']
.

1.3、action=“store_const”

使用 const 为命令行参数指定一个常量值,必须和 const 参数同时使用,使用这个模式后命令行参数不能赋值

#content of conftest.py
def pytest_addoption(parser):
    parser.addoption(
        "--const1",action="store_const",default='this is default value',const='这是为命令行指定的常量值。。。',
        help='备注信息'
    )
@pytest.fixture()
def const1(request):
    return request.config.getoption("--const1")
# content of test_param1.py
def test1(const1):
    print(const1)
const1参数带value值运行时报错:
D:\pytest\learn01>pytest -s -q --const1=1111   e/test_param1.py
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument --const1: ignored explicit argument '1111'

不带值运行时,输出指定的值:
D:\pytest\learn01>pytest -s -q --const1   e/test_param1.py
这是为命令行指定的常量值。。。
.
1 passed in 0.08s

1.4、action=“append_const”

存储一个列表,使用 const 为命令行参数指定一个常量值,并将 default 默认值和 const 常量值添加到列表中,这个模式可以同时多次使用自定义参数,但是还是不能赋值,只能使用常量;

def pytest_addoption(parser):
    parser.addoption(
        "--const2",action="append_const",default=['this is default value'],const='这是为命令行指定的常量值。。。',
        help='备注信息')
输出:
D:\pytest\learn01>pytest -s -q --const2   e/test_param1.py
['this is default value', '这是为命令行指定的常量值。。。']
.
1 passed in 0.06s

2、type

type 的类型可以是 python 的基础类型,比如:int,str,float,list 等类型,如果不指定类型的话,pytest会把接受到的参数值都默认为 str 类型,所以我们有时需要指定参数的类型:
注意:在使用 type 指定类型时,也需要把 default 的类型修改为同样的类型!

def pytest_addoption(parser):
    parser.addoption(
        "--cmdopt1", action="store", default=12345,type=int, help="my option: type1 or type2"
    )
 执行命令:
D:\pytest\learn01>pytest -s -q --cmdopt1=222   e/test_param1.py
222
.
1 passed in 0.01s

3、choice

choices 可以指定几个值,自定义参数必须在这几个值中选择一个,否则会报错

def pytest_addoption(parser):
    parser.addoption(
        "--cmdopt2", action="store", default=12345, type=int, choices=[111,222,333,4444],
        help="my option: type1 or type2"
    )
执行命令,指定参数不在choices中时,报错
D:\pytest\learn01>pytest -s -q --cmdopt2=12   e/test_param1.py
ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: argument --cmdopt2: invalid choice: 12 (choose from 111, 222, 333, 4444)

原文链接:https://blog.csdn.net/waitan2018/article/details/104320927

— 于 共写了3600个字
— 标签:

评论已关闭。