參數(shù)化使用
- @pytest.mark.parametrize(argnames, argvalues)
- argnames:要參數(shù)化的變量砌滞,string(逗號(hào)分割)镰踏,list,tuple
- argvalues:參數(shù)化的值蒂培,list再愈,list[tuple]
import pytest
# 參數(shù)使用string
@pytest.mark.parametrize("a,b", [(10,20), (30,40)])
def test_param(a, b):
print(a+b)
# 參數(shù)使用list
@pytest.mark.parametrize(["a","b"], [(10,20), (30,40)])
def test_param(a, b):
print(a+b)
# 參數(shù)使用tuple
@pytest.mark.parametrize(("a","b"), [(10,20), (30,40)])
def test_param(a, b):
print(a+b)
參數(shù)使用list,就可以使用list的方法护戳,比如 pop翎冲、append、insert媳荒、reverse等抗悍;參數(shù)使用tuple就可以使用tuple的方法,如 index肺樟、count檐春;參數(shù)使用string同上。
yaml 數(shù)據(jù)參數(shù)化
yaml 安裝
pip install PyYaml
- 在pycharm中的設(shè)置里面進(jìn)行安裝:Project-> Project Interpreter-> + -> 搜索PyYaml -> 點(diǎn)擊Install Package
yaml 實(shí)現(xiàn) list
- 10
- 20
- 30
yaml 實(shí)現(xiàn)字典
by: id
locator: name
action: click
yaml 進(jìn)行嵌套
-
- by: id
- locator: name
- action: click
companies:
- 1
id: 1
name: company1
price: 200w
- 2
id: 2
name: company2
price: 500w
結(jié)果為:companies: [{id:1, name:company1, price:200w}, {id:2, name:company2, price:500w}]
加載文件
yaml.safe_load(open("./data.yaml"))
實(shí)戰(zhàn)
data.yaml 文件內(nèi)容如下:
-
- 10
- 20
-
- 30
- 40
-
- 15
- 13
測(cè)試用例如下:
import pytest
import yaml
class TestData:
@pytest.mark.parametrize("a,b",yaml.safe_load(open("data.yaml")))
def test_data(self, a, b):
print(a+b)
運(yùn)行結(jié)果如下:
collecting ... collected 3 items
param_p.py::TestData::test_data[10-20]
param_p.py::TestData::test_data[30-40]
param_p.py::TestData::test_data[15-13]
============================== 3 passed in 0.07s ==============================
Process finished with exit code 0
PASSED [ 33%]30
PASSED [ 66%]70
PASSED [100%]28
下一節(jié):數(shù)據(jù)驅(qū)動(dòng)(Yaml么伯、JSON疟暖、Excel、CSV)田柔,包括使用外部數(shù)據(jù)自動(dòng)創(chuàng)建用例