pytest十二:cmd命令行参数

命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在 cmd 执行”pytest —html=report.html”,这里面的”—html=report.html“就是从命令行传入的参数对应的参数名称是 html,参数值是 report.html

conftest 配置参数
首先需要在 conftest.py 添加命令行选项,命令行传入参数”—cmdopt“, 用例如果需要用到从命令行传入的参数,就调用 cmdopt函数:

 

import pytest


def pytest_addoption(parser):
parser.addoption(
'--cmdopt', action='store', default='type1', help='myoption: type1 or type2'
)

@pytest.fixture()
def cmdopt(request):
return request.config.getoption('--cmdopt')


测试用例编写案例

import pytest

def test_answer(cmdopt):
if cmdopt == 'type1':
print('first')
elif cmdopt =='type2':
print('second')
assert 0

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

 



带参数启动
1.如果不带参数执行,那么传默认的 default=”type1”,接下来在命令行带上参数去执行
> pytest -s test_19_cmd.py --cmdopt=type2
2.命令行传参数有两种写法,迓有一种分成 2 个参数也可以的,参数和名称用空格隔开
> pytest -s test_19_cmd.py --cmdopt type2






原文地址:https://www.cnblogs.com/zhongyehai/p/9685862.html