列表:只有一个参数data
import pytest
#列表 这样会运行两次
data=['123','456']
@pytest.mark.parametrize('pwd',data)
def test1(pwd):
print(pwd)
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items
test05.py::test1[123] PASSED [ 50%]123
test05.py::test1[456] PASSED [100%]456
============================== 2 passed in 0.06s ==============================
元组参数化:
#元组 也是遍历两次 123,456
data2=[
(1,2,3),
(4,5,6)
]
@pytest.mark.parametrize('a,b,c',data2)
def test2(a,b,c):
print(a,b,c)
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items
test05.py::test2[1-2-3] PASSED [ 50%]1 2 3
test05.py::test2[4-5-6] PASSED [100%]4 5 6
============================== 2 passed in 0.25s ==============================
Process finished with exit code 0
字典类型:
#字典类型
data3=(
{'user':1,
'pwd':2
},
{'age':21,
'name':'byzhang'
}
)
@pytest.mark.parametrize('dic',data3)
def test3(dic):
print(dic)
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items
test05.py::test3[dic0] PASSED [ 50%]{'user': 1, 'pwd': 2}
test05.py::test3[dic1] PASSED [100%]{'age': 21, 'name': 'byzhang'}
============================== 2 passed in 0.06s ==============================
Process finished with exit code 0
添加id方式来执行参数化:
#添加id id的值可以自定义 只要方便理解每个值是干什么的就行
data01=[
pytest.param(1,2,3,id="a+b:pass"),
pytest.param(4,5,10,id="a+b:fail")
]
def add(a,b):
return a+b
#接下来看怎么测试
class TestParametrize(object):
@pytest.mark.parametrize('a,b,expect',data01)
def test_paramerrize(self,a,b,expect):
assert add(a,b)==expect
运行结果:
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1 -- C:\ProgramData\Anaconda3\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.3', 'Platform': 'Windows-10-10.0.17763-SP0', 'Packages': {'pytest': '6.2.1', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0'}, 'JAVA_HOME': 'E:\\Program Files\\jdk1.8.0_171'}
rootdir: C:\Users\Administrator\pythonxiangmu\seelenium_project\testcases\pytest, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0
collecting ... collected 2 items
test05.py::TestParametrize::test_paramerrize[a+b:pass]
test05.py::TestParametrize::test_paramerrize[a+b:fail] PASSED [ 50%]FAILED [100%]
test05.py:51 (TestParametrize.test_paramerrize[a+b:fail])
9 != 10
Expected :10
Actual :9
<Click to see difference>
self = <seelenium_project.testcases.pytest.test05.TestParametrize object at 0x000002328970AA58>
a = 4, b = 5, expect = 10
@pytest.mark.parametrize('a,b,expect',data01)
def test_paramerrize(self,a,b,expect):
> assert add(a,b)==expect
E assert 9 == 10
test05.py:54: AssertionError
Assertion failed
Assertion failed
后面会具体运行 在这里就是举例简单介绍一下。