1. 程式人生 > 其它 >pytest--引數化

pytest--引數化

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%]