1.参数化方式一
import pytest
# @pytest.mark.parametries("a,b",[(1,2),(2,3),(3,4)])
# @pytest.mark.parametries(("a","b"),[(1,2),(2,3),(3,4)])
class TestDemo:
@pytest.mark.parametrize("a,b",[(1,2),(2,3),(3,4)])
def test_param(self,a,b):
if a==b:
return True
else:
return False
if __name__ == '__main__':
pytest.main(['test_2.py','-v'])
2.参数化方式二--文件输入
安装PYYML:pip install PYYML
新建data.yml文件 -:列表 key:value 集合
-
- 10
- 20
-
- 30
- 40
-
- 5
- 6
import pytest
import yaml
class TestData:
@pytest.mark.parametrize(("a", "b"), yaml.safe_load(open("./data.yml")))
def test_data(self, a, b):
print(a + b)
if __name__ == '__main__':
pytest.main(['test_3.py','-v'])
结果:
collecting … collected 3 items
test_3.py::TestData::test_data[10-20] PASSED [ 33%]
test_3.py::TestData::test_data[30-40] PASSED [ 66%]
test_3.py::TestData::test_data[5-6] PASSED [100%]
原创文章,作者:bd101bd101,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/245318.html