参数化
pytest使用parametrize标记实现参数化
@pytest.mark.parametrize(argnames,argvalues)
parametrize()的第一个参数时用逗号分割的的字符串列表,第二个参数时一个值列表
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
import sys
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
def test_one(a,b):
assert a == b
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: /media/_dde_data/python
collected 3 items
test_001.py FF. [100%]
========================================== FAILURES ==========================================
_______________________________________ test_one[1-2] ________________________________________
a = 1, b = 2
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
def test_one(a,b):
> assert a == b
E assert 1 == 2
test_001.py:8: AssertionError
_______________________________________ test_one[a-b] ________________________________________
a = 'a', b = 'b'
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)])
def test_one(a,b):
> assert a == b
E AssertionError: assert 'a' == 'b'
E - b
E + a
test_001.py:8: AssertionError
================================== short test summary info ===================================
FAILED test_001.py::test_one[1-2] - assert 1 == 2
FAILED test_001.py::test_one[a-b] - AssertionError: assert 'a' == 'b'
================================ 2 failed, 1 passed in 0.20s =================================
ids
参数化装饰器有一个额外的参数ids,可以标识每一个测试用例,自定义测试数据结果的显示,为了增加可读性,我们可以标记每一个测试用例使用的测试数据是什么,适当的增加一些说明
#!/usr/bin/python3
#-*- conding:utf-8 -*-
import pytest
import sys
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
def test_one(a,b):
assert a == b
==================================== test session starts =====================================
platform linux -- Python 3.5.3, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /media/_dde_data/python
collected 3 items
test_001.py::test_one[1 != 2] FAILED [ 33%]
test_001.py::test_one[a != b] FAILED [ 66%]
test_001.py::test_one[False = False] PASSED [100%]
========================================== FAILURES ==========================================
______________________________________ test_one[1 != 2] ______________________________________
a = 1, b = 2
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
def test_one(a,b):
> assert a == b
E assert 1 == 2
E +1
E -2
test_001.py:8: AssertionError
______________________________________ test_one[a != b] ______________________________________
a = 'a', b = 'b'
@pytest.mark.parametrize("a,b",[(1,2),('a','b'),(False,False)],ids=["1 != 2","a != b","False = False"])
def test_one(a,b):
> assert a == b
E AssertionError: assert 'a' == 'b'
E - b
E + a
test_001.py:8: AssertionError
================================== short test summary info ===================================
FAILED test_001.py::test_one[1 != 2] - assert 1 == 2
FAILED test_001.py::test_one[a != b] - AssertionError: assert 'a' == 'b'
================================ 2 failed, 1 passed in 0.21s =================================