当前位置: 首页 > 工具软件 > pytest > 使用案例 >

pytest 命令行注册函数之 pytest_addoption

容寒
2023-12-01

简介

pytest_addoption 允许用户自定义注册一个命令行参数,方便用户通过命令行参数的形式给 pytest 传递不同的参数进行不同测试场景的切换。
通常与内置 fixture 之 pytestconfig 配合使用。即 pytest_addoption 负责注册命令行参数,pytestconfig 负责读取命令行参数传入的值。

参数说明

pytest_addoption 注册函数中 parser.addoption(*args, **kwargs) 的参数:

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

参数举例演示

action=‘store’

action=‘store’:默认,只存储参数的值,可以存储任何类型的值,此时的 default 也可以是任何类型的值。并且命令行参数即使重复多次也只会生效一个,最后一个覆盖之前传入的值

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--env',
					 action='store',
					 default='test_env',
					 help='test_env:表示测试环境,默认值为 test_env')

@pytest.fixture
def env(pytestconfig):
	env = pytestconfig.getoption('--env')
	return env

''' test_01.py '''
def test_env(env):
	print('当前测试环境为:{}'.format(env))

''' main.py '''
if __name__ == '__main__':
	pytest.main('-vs')

========== output ========
.当前测试环境为:test env

或

if __name__ == '__main__':
	pytest.main(['--env=http://www.baidu.com'])

========== output ========
.当前测试环境 env: http://www.baidu.com

action=‘append’

action=‘append’:存储的是一个 list 列表值,与此同时,default 也要求传入一个列表值。用例运行时,pytest 会将 default 中的默认参数与传入的自定义参数放在一个列表中

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--input_args',
					 action='append',
					 default=[1, 2, 3],
					 help='input_args:表示测试数据,默认值为 [1, 2, 3]')

@pytest.fixture
def input_args(pytestconfig):
	input_args= pytestconfig.getoption('--input_args')
	return input_args

''' test_01.py '''
def test_input_args(input_args):
	print('当前测试参数input_args: {}'.format(input_args))

''' main.py '''
if __name__ == '__main__':
	pytest.main('-vs')

========== output ========
.当前测试参数 input_args: [1, 2, 3]

或

if __name__ == '__main__':
	pytest.main(['-vs', '--input_args=http://www.baidu.com', '--input_args=http://jd.com'])

========== output ========
.当前测试参数 input_args: [1, 2, 3, 'http://www.baidu.com', 'http://jd.com']

action=‘store_const’

action=‘store_const’:使用 const 为命令行参数指定一个常量值,使用此参数时不允许赋值
注意:const 为必须使用参数

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--const_value',
					 action='store_const',
					 const=r'C:\Users\Administrator\Desktop',
					 help='const_value:表示默认存储路径,为:C:\Users\Administrator\Desktop')

@pytest.fixture
def const_value(pytestconfig):
	const_value= pytestconfig.getoption('--const_value')
	return const_value

''' test_01.py '''
def test_const_value(const_value):
	print('当前存储路径: {}'.format(const_value))

''' main.py '''
if __name__ == '__main__':
	pytest.main(['-vs'])

========== output ========
.当前存储路径: 不设置存储路径

或

if __name__ == '__main__':
	pytest.main(['-vs', '--const_value'])

========== output ========
.当前存储路径: C:\Users\Administrator\Desktop

action=‘append_const’

action=‘store_const’:存储一个列表,使用 const 为命令行参数指定一个常量值,并将 constdefault 添加到列表中。此时定义的命令参数允许多次使用,但使用此参数时不允许赋值,只能使用其常量
注意:const 为必须使用参数

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--const_value',
					 action='append_const',
					 default='我是默认值...',
					 const='我是常量...',
					 help='const_value:将常量通过命令 --const_value 添加到 pytest 配置中')

@pytest.fixture
def const_value(pytestconfig):
	const_value= pytestconfig.getoption('--const_value')
	return const_value

''' test_01.py '''
def test_const_value(const_value):
	print('当前列表内容: {}'.format(const_value))

''' main.py '''
if __name__ == '__main__':
	pytest.main(['-vs'])

========== output ========
.当前列表内容: ['我是默认值...']

或

if __name__ == '__main__':
	pytest.main(['-vs', '--const_value'])

========== output ========
.当前列表内容: ['我是默认值...', '我是常量...', '我是常量...']

type

type:type 类型包含 python 的数据类型,如 str, int, list, dict, float 等。默认:str。如果指定 type 的类型,则 default 也要指定相同的类型

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--curr_args',
					 action='store',
					 default=123,
					 type=int,
					 help='curr_args:表示测试环境参数,默认值为 123')

@pytest.fixture
def curr_args(pytestconfig):
	curr_args= pytestconfig.getoption('--curr_args')
	return curr_args

''' test_01.py '''
def test_curr_args(curr_args):
	print('当前测试参数 curr_args 为:{}'.format(curr_args))

''' main.py '''
if __name__ == '__main__':
	pytest.main()

========== output ========
.当前测试参数 curr_args 为:123

或

if __name__ == '__main__':
	pytest.main(['--curr_args=1234567890'])

========== output ========
.当前测试参数 curr_args 为:1234567890


# 当传入的参数不是 int 时,将报错:

if __name__ == '__main__':
	pytest.main(['--curr_args=aaa1234567890'])

========== output ========
ERROR: usage: main.py [options] [file_or_dir] [file_or_dir] [...]
main.py: error: argument --input_args: invalid int value: 'aaa1234567890'

choices

choices:choices 参数存储容器,当前命令的参数值只能从此容器中取出,否则将报错

''' conftest.py '''
import pytest

def pytest_addoption(parser):
	parser.addoption(name='--program_lan',
					 action='store',
					 default=None'我是默认值...',
					 choices=['python', 'java', 'c++'],
					 help='program_lan:表示编程语言环境,默认 None')

@pytest.fixture
def program_lan(pytestconfig):
	program_lan= pytestconfig.getoption('--program_lan')
	return program_lan

''' test_01.py '''
def test_program_lan(program_lan):
	print('编程语言环境 program_lan 为:{}'.format(program_lan))

''' main.py '''
if __name__ == '__main__':
	pytest.main(['-vs'])

========== output ========
.编程语言环境 program_lan为:我是默认值...

或

if __name__ == '__main__':
	pytest.main(['-vs', '--input_args=python'])

========== output ========
.编程语言环境 program_lan 为:python


# 当传入的参数不在 choices 提供的选项中时,将报错:

if __name__ == '__main__':
	pytest.main(['--program_lan =aaa1234567890'])

========== output ========
ERROR: usage: main.py [options] [file_or_dir] [file_or_dir] [...]
main.py: error: argument --input_args: invalid choice: 'aaa1234567890' (choose from 'python', 'java', 'c++')

 类似资料: