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 ======================================================================
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,