在test_example.py 檔案中編寫如下程式碼:
def add(a,b):
return (a+b)
def test_1():
assert add(3,5)==8
def test_2():
assert add(2,4)==7
def test_3():
assert add(5,7)==12
使用pytest -s 執行結果如下:兩個通過,一個失敗
(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo
plugins: html-2.1.1, metadata-1.10.0
collected 3 items
test_example.py .F.
=============================================================================== FAILURES ===============================================================================
________________________________________________________________________________ test_2 ________________________________________________________________________________
def test_2():
> assert add(2,4)==7
E assert 6 == 7
E + where 6 = add(2, 4)
test_example.py:11: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_example.py::test_2 - assert 6 == 7
===================================================================== 1 failed, 2 passed in 0.16s ======================================================================
請看如下程式碼:
import pytest
def add(a,b):
return (a+b)
@pytest.mark.parametrize("a,b,c",[(3,5,8),(2,4,7),(5,7,12)])
def test_1(a,b,c):
assert add(a,b)==c
使用pytest -s執行結果如下:這裏雖然只寫了一個test函數,但是結果仍然顯示三個用例,是因爲參數化的時候填寫了三個元組的數據,這就是參數化,其實叫數據驅動可能更好理解一些,這樣可以節省大量的程式碼
(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo
plugins: html-2.1.1, metadata-1.10.0
collected 3 items
test_example.py .F.
=============================================================================== FAILURES ===============================================================================
____________________________________________________________________________ test_1[2-4-7] _____________________________________________________________________________
a = 2, b = 4, c = 7
@pytest.mark.parametrize("a,b,c",[(3,5,8),(2,4,7),(5,7,12)])
def test_1(a,b,c):
> assert add(a,b)==c
E assert 6 == 7
E + where 6 = add(2, 4)
test_example.py:9: AssertionError
======================================================================= short test summary info ========================================================================
FAILED test_example.py::test_1[2-4-7] - assert 6 == 7
===================================================================== 1 failed, 2 passed in 0.16s ======================================================================
請看如下程式碼:表示a可以取值2,4,6,而b可以取值1,3,5,而在執行測試用例的時候是將a,b全部可能的值組合起來的
import pytest
@pytest.mark.parametrize("a",[2,4,6])
@pytest.mark.parametrize("b",[1,3,5])
def test_1(a,b):
print(a,b)
使用pytest -s執行結果如下:
(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo
plugins: html-2.1.1, metadata-1.10.0
collected 9 items
test_example.py 2 1
.4 1
.6 1
.2 3
.4 3
.6 3
.2 5
.4 5
.6 5
.
========================================================================== 9 passed in 0.07s ===========================================================================
如下程式碼:
import pytest
@pytest.mark.parametrize("a,b",[(1,2),(3,4),(5,6)])
class TestExample(object):
def test_01(self,a,b):
print(a,b)
def test_02(self,a,b):
print(b,a)
使用pytest -s執行結果如下,顯示執行了6個用例
(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo
plugins: html-2.1.1, metadata-1.10.0
collected 6 items
test_example.py 1 2
.3 4
.5 6
.2 1
.4 3
.6 5
.
========================================================================== 6 passed in 0.06s ===========================================================================
程式碼如下:
import pytest
@pytest.mark.parametrize("a,b",[(1,2),(3,4),pytest.param(5,6,marks=pytest.mark.xfail),pytest.param(7,8,marks=pytest.mark.skip)])
class TestExample(object):
def test_01(self,a,b):
print(a,b)
使用pytest -s執行結果如下:
(PytestEnv) G:\lamb_source\pytest_example_for_full_documentation\test_demo>pytest -s
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: G:\lamb_source\pytest_example_for_full_documentation\test_demo
plugins: html-2.1.1, metadata-1.10.0
collected 4 items
test_example.py 1 2
.3 4
.5 6
Xs
=============================================================== 2 passed, 1 skipped, 1 xpassed in 0.05s ================================================================