首页 > 其他 > 详细

pytest的参数化

时间:2019-05-19 14:20:22      阅读:363      评论:0      收藏:0      [点我收藏+]

参数化有两种方式:

1、

@pytest.mark.parametrize

2、利用conftest.py里的

pytest_generate_tests

 

1中的例子如下:

@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6), ("6*9", 42)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

 2中的例子(自己定义参数化,pytest_generate_tests 是在收集测试方法时会被调用)的:

conftest.py 

def pytest_addoption(parser):
    parser.addoption(
        "--stringinput",
        action="append",
        default=[],
        help="list of stringinputs to pass to test functions",
    )


def pytest_generate_tests(metafunc):
    if "stringinput" in metafunc.fixturenames:
        metafunc.parametrize("stringinput", metafunc.config.getoption("stringinput"))

a_test.py
def test_valid_string(stringinput):
    assert stringinput.isalpha()

执行测试:
pytest -q --stringinput="hello" --stringinput="world" a_test.py


参数化参考地址:
https://docs.pytest.org/en/latest/parametrize.html#pytest-mark-parametrize-parametrizing-test-functions

pytest的参数化

原文:https://www.cnblogs.com/yingchen/p/10889025.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!