微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest的参数化

参数化有两种方式:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐