pytest数据驱动pytest.mark.parametrize
parametrize
支持元组,列表
支持列表嵌套列表,列表嵌套元组,列表嵌套字典
支持类和函数的参数化
1、单个变量
@pytest.mark.parametrize('user', ("zhangsan", "lisi")) def test_01(self,user): print(user)
2、单个变量参数为 列表
datatest04=['zhangsan','lisi'] @pytest.mark.parametrize('user', datatest04) def test_04(self,user): print(user)
3、单个变量参数为 元组
datatest08=("zhangsan","lisi") @pytest.mark.parametrize("data", datatest08) def test_08(self,data): print(data)
4、多个变量
@pytest.mark.parametrize('user,password',[("zhangsan","111111"),("lisi","222222")]) def test_02(self,user,password): print(user,password)
5、多个变量组成列表嵌套元组
data =[("zhangsan","111111111"),("lisi","2222222")] @pytest.mark.parametrize('user,password',data) def test_03(self,user,password): print(user,password)
6、多个变量组成列表嵌套列表
data =[["zhangsan","111111111"],["lisi","2222222"]] @pytest.mark.parametrize('user,password',data) def test_05(self,user,password): print(user,password)
7、针对类参数化--不知道应用场景
@pytest.mark.parametrize('user,password',[("zhangsan","111111"),("lisi","222222")]) class Testdemo1(object): def test_01(self,user,password): print(user,password) def test_02(self,user,password): print(user,password)
8、组合函数-笛卡尔集
data1 =["zhangsan","lisi"] data2 = ["1111111", "2222222"] @ pytest.mark.parametrize('user', data1) @pytest.mark.parametrize('password',data2) def test_06(self,user,password): print(user,password)
错误写法
data1 =["zhangsan","111111111"] data2 = ["lisi", "2222222"] @ pytest.mark.parametrize('user,password', data1) @pytest.mark.parametrize('user,password',data2) def test_06(self,user,password): print(user,password)
9、列表嵌套字典
datatest07 = [{"user":"zhangsan", "password":"111111111"},{"user":"lisi","password":"222222"}] @pytest.mark.parametrize("data", datatest07) def test_07(self,data): print(data) print(data["user"],data["password"])
错误写法
datatest07 = [{"user":"zhangsan", "password":"111111111"},{"user":"lisi","password":"222222"}] @pytest.mark.parametrize("user,password", datatest07) def test_07(self,data): print(data) print(data["user"],data["password"])
上班求生存,下班求发展