pytest-09-参数化parametrize+命令行传参

1、参数化parametrize

(1)测试用例参数化使用装饰器 pytest.mark.parametrize

参数化
参数化执行结果

(2)参数组合:获取多个参数化参数的所有组合

参数组合实例
参数的所有组合

2、命令行传参

根据命令行选项将不同的值传给测试函数

(1)conftest.py添加命令选项,命令行传入参数'--cmdfun'(需要建立cmdfun函数后才能调用)

import pytest

# --------添加命令行选项-------------

def pytest_addoption(parser):

    parser.addoption(

        '--cmdopt', action='store', default='type1', help='my option: type1 or type2'

    )

@pytest.fixture

def cmdopt(request):

    return request.config.getoption('--cmdopt')

(2)创建test_sample.py添加用例

import pytest

def test_answer(cmdopt):

    if cmdopt == 'type1':

        print('first')

    elif cmdopt == 'type2':

        print('second')

    assert 0

if __name__ == '__main__':

    pytest.main(['-s', 'test_sample.py'])

(3)启动

pycharm中直接执行即可

不带参数启动:pytest -s test_sample.py 默认启动第一个

带参数启动:pytest -s test_sample.py --cmdopt=type2 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容