@pytest.mark.parametrize(argnames,argvalues)
argnames:要参数化的变量,string,list,tuple
argvalues:参数化的值,list,list[tuple]
使用stringimport pytest
@pytest.mark.parametrize("a,b",[(10,20),(10,30)])
def test_case1(a,b):
print(a+b)
if __name__ == '__main__':
pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
使用tuple
import pytest
@pytest.mark.parametrize(("a","b"),[(10,20),(10,30)])
def test_case1(a,b):
print(a+b)
if __name__ == '__main__':
pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
使用list
import pytest
class Testdata:
@pytest.mark.parametrize(["a","b"],[(10,20),(10,5),(10,7)])
def test_demo1(self,a,b):
a=10
b=20
print(a+b)
if __name__ == '__main__':
pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])
yaml的基本使用
安装 pyyaml 库
pip install pyyamlyaml实现list
list
- 10
- 20
- 30
yaml实现字典
dict
by:id
locator:name
action:click
yaml进行嵌套
-
- by:id
- locator:name
- action: click
companies:
-
id:1
name:company1
price:200W
-
id:2
name:company2
price:500W
加载yaml文件
yaml.safe_load(open("./data.yaml"))
pytest数据参数化加载yaml
@pytest.mark.parametrize(["a","b"],yaml.safe_load(open("./data.yaml")))
def test_param(self,a,b):
print(a+b)
创建data.yaml文件
- - 10 - 10 - - 20 - 10 - - 10 - 30
import pytest
import yaml
class Testdata:
@pytest.mark.parametrize(["a","b"],yaml.safe_load(open(r"E:pythoncodepython_testdata.yaml")))
def test_demo1(self,a,b):
print(a+b)
if __name__ == '__main__':
pytest.main(["-s","-v",r"E:pythoncodepython_test2021103_test01.py"])



