在测试过程中,参数化是必不可少的功能,本文就讨论下pytest的几种参数化方法
@pytest.mark.parametrize:参数化测试函数
1.内置的pytest.mark.parametrize装饰器支持测试函数的参数化基本用法
例如:
@pytest.mark.parametrize("input,expect",[("3+5",8),("5+5",9),("4+9",12),("10+21",31)])
def test_param(input,expect):
assert eval(input)==expect
结果:成功2个失败2个,但是需要注意的是,@pytest.mark.parametrize("test_input,expected",[("3+5",8),("2+4",6),("6*9",42)])里面的"test_input,expected"一定要和test_add(test_input,expected)当中的参数名称一致,否则,将会出错。
2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail
@pytest.mark.parametrize("input,expect",[("3+5",8),("5+5",9),("4+9",12),pytest.param("10+21",31,marks=pytest.mark.xfail)])
def test_param(input,expect):
assert eval(input)==expect
3.参数组合测试:
@pytest.mark.parametrize("x",[1,2,3,4])
@pytest.mark.parametrize("y",[3,4])
def test_param(x,y):
print("参数组合%s,******,%s"%(x,y))
结果会测试8次组合结果,如图:
原文地址:https://www.cnblogs.com/qixc/p/12168734.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。