在做自动化测试的时候,很多时候我们测试步骤一样,但是测试数据不一样,此时就需要用到参数化。
那参数化具体是把什么进行参数化了,也就是参数化的本质就是对列表中的对象依次循环,然后赋值给对应的参数,在python中可以循环的对象有列表,元组,字典
一、对列表进行参数化
import pytest
def add(a,b):
return a+b
@pytest.mark.parametrize('a,b,excpet',[[1,2,3],[3,6,9],[7,5,10]])
def test_add(a,b,excpet):
assert a+b==excpet
二、对元祖进行参数化
import pytest
def add(a,b):
return a+b
@pytest.mark.parametrize('a,b,excpet',[(1,2,3),(3,6,9),(7,5,10)])
def test_add(a,b,excpet):
assert a+b==excpet
三、对字典进行参数化
import pytest
def add(a,b):
return a+b
@pytest.mark.parametrize('data',[{"a":1,"b":2,"excpet":3},{"a":4,"b":5,"excpet":9}])
def test_add(data):
assert data['a']+data['b']==data['excpet']