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 

你可能感兴趣的:(pytest-09-参数化parametrize+命令行传参)