pytest十二:cmd命令行参数

本文详细介绍了如何在pytest中使用命令行参数来动态调整测试行为,包括通过conftest.py文件配置参数,并在测试用例中利用这些参数进行条件判断,实现灵活的测试场景。
命令行参数是根据命令行选项将不同的值传递给测试函数,比如平常在 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值