pytest(九)--参数化parametrize

前言

pytest.mark.parametrize装饰器可以实现测试用例参数化。

parametrizing

1.这里是一个实现检查一定的输入和期望输出测试功能的典型例子

 

# coding:utf-8
import pytest
@pytest.mark.parametrize("test_input,expect",
                    [("3+5",8),("2+4",6),("6*9",42),])
def test_params(test_input,expect):
    assert eval(test_input)==expect
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

 

运行结果

 

 

 在这个例子中设计的,只有一条输入/输出值的简单测试功能。和往常一样函数的参数,你可以在运行结果看到在输入和输出值。

2.它也可以标记单个测试实例在参数化,例如使用内置的mark.xfail

# coding:utf-8
import pytest
@pytest.mark.parametrize("test_input,expect",
                    [("3+5",8),
                     pytest.param("2+4",6,marks=pytest.mark.xfail),
                     pytest.param("6*9",42,marks=pytest.mark.xfail),])
def test_params(test_input,expect):
    print("-------开始执行测试用例-------")
    assert eval(test_input)==expect
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])  

运行结果 

 

 

 

 标记为失败的用例,预期结果是失败,实际运行也是失败,显示xfailed

参数组合

1.若要获得多个参数化参数的所有组合,可以堆叠参数化装饰器

# coding:utf-8
import pytest
@pytest.mark.parametrize("x",[0,1])
@pytest.mark.parametrize("y",[2,3])
def test_a(x,y):
    #print("测试数据组合:x—>%s,y—>%s"%(x,y))
    print("测试参数组合,x:{},y:{}".format(x,y))
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

 运行结果:

 

 

 

如下是多参数?

 

 

 

posted on 2020-07-29 17:22  星空6  阅读(275)  评论(0编辑  收藏  举报

导航