天天看點

pytest(1)-安裝以及快速入門

1、pytest 的特點

(1) 非常容易上手,入門簡單,文檔豐富,文檔中有很多執行個體可以參考

(2) 能夠支援簡單的單元測試和複雜的功能測試

(3) 支援參數化

(4) 執行測試過程中可以将某些測試跳過(skip),或者對某些預期失敗的case标記成失敗

(5) 支援重複執行(rerun)失敗的 case

(6) 支援運作由 nose, unittest 編寫的測試 case

(7) 可生成 html 報告

(8) 友善的和持續內建工具 jenkins 內建

(9) 可支援執行部分用例

(10) 具有很多第三方插件,并且可以自定義擴充

2、pytest 的安裝

執行如下指令進行安裝

pip install pytest
           

使用如下指令檢視安裝pytest的版本

pytest --version
           

如下:

pytest(1)-安裝以及快速入門

3、建立第一個測試用例

  • 建立一個test_example.pywenjian ,内容編寫如下:
def add(a,b):
    return a+b

def test_add():
    assert(add(10,20)==50)
           
  • 在指令行執行 pytest

    執行結果如下:

(demo) (base) [[email protected] demo]# pytest
========================================================================== test session starts ==========================================================================
platform linux -- Python 3.9.5, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /opt/demo
plugins: metadata-1.11.0, html-3.1.1
collected 1 item                                                                                                                                                        

test_example.py F                                                                                                                                                 [100%]

=============================================================================== FAILURES ================================================================================
_______________________________________________________________________________ test_add ________________________________________________________________________________

    def test_add():
>       assert(add(10,20)==50)
E       assert 30 == 50
E        +  where 30 = add(10, 20)

test_example.py:5: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_example.py::test_add - assert 30 == 50
=========================================================================== 1 failed in 0.02s ===========================================================================
(demo) (base) [[email protected] demo]#
           

繼續閱讀