和Unittest一樣,Pytest是另一個Python語言的單元測試架構,與Unittest相比它的測試用例更加容易編寫、運作方式更加靈活、報錯資訊更加清晰、斷言寫法更簡潔并且它可以運作有unittest和nose編寫的測試用例。
Pytest 安裝
啟動指令行,在指令行中使用pip工具安裝pytest,如圖所示。
C:\Users\Administrator>pip install -U pytest
Collecting pytest
Using cached pytest-5.4.1-py3-none-any.whl (246 kB)
Requirement already satisfied, skipping upgrade: pluggy<1.0,>=0.12 in c:\program files\python38\lib\site-packages (from pytest) (0.13.1)
Requirement already satisfied, skipping upgrade: atomicwrites>=1.0; sys_platform == "win32" in c:\program files\python38\lib\site-packages (from pytest) (1.3.0)
Requirement already satisfied, skipping upgrade: colorama; sys_platform == "win32" in c:\program files\python38\lib\site-packages (from pytest) (0.4.3)
Requirement already satisfied, skipping upgrade: wcwidth in c:\program files\python38\lib\site-packages (from pytest) (0.1.8)
Requirement already satisfied, skipping upgrade: packaging in c:\program files\python38\lib\site-packages (from pytest) (20.3)
Requirement already satisfied, skipping upgrade: attrs>=17.4.0 in c:\program files\python38\lib\site-packages (from pytest) (19.3.0)
Requirement already satisfied, skipping upgrade: more-itertools>=4.0.0 in c:\program files\python38\lib\site-packages (from pytest) (8.2.0)
Requirement already satisfied, skipping upgrade: py>=1.5.0 in c:\program files\python38\lib\site-packages (from pytest) (1.8.1)
Requirement already satisfied, skipping upgrade: six in c:\program files\python38\lib\site-packages (from packaging->pytest) (1.14.0)
Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in c:\program files\python38\lib\site-packages (from packaging->pytest) (2.4.6)
Installing collected packages: pytest
Successfully installed pytest-5.4.1
代碼示例
建立一個python檔案,并寫入如下代碼:
def test_equal():
assert(1,2,3)==(1,2,3)
然後在指令行運作該檔案,執行指令為 pytest xxx.py,執行結果如圖
C:\Users\Administrator>pytest C:\Users\Administrator\Desktop\123.py
===================================== test session starts ==================================================
platform win32 -- Python 3.8.1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\Administrator
collected 1 item
Desktop\123.py . [100%]
====================================== 1 passed in 0.09s ==================================================
如果想看到詳細的執行結果,可以給執行指令加上參數 -v或者–verbose,即pytest -v xxx.py,執行結果如圖
C:\Users\Administrator>pytest -v C:\Users\Administrator\Desktop\123.py
======================================= test session starts =================================================
platform win32 -- Python 3.8.1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 -- c:\program files\python38\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Administrator
collected 1 item
Desktop/123.py::test_equal PASSED [100%]
============================================ 1 passed in 0.04s ===============================================
我們在看一個執行失敗的例子,再建立一個py檔案,寫入如下代碼:
def test_equal():
assert(1,2,3)==(3,2,1)
C:\Users\Administrator>pytest -v C:\Users\Administrator\Desktop\123.py
=========================================== test session starts ==============================================
platform win32 -- Python 3.8.1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 -- c:\program files\python38\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\Administrator
collected 1 item
Desktop/123.py::test_equal FAILED [100%]
============================================= FAILURES =======================================================
____________________________________________ test_equal ______________________________________________________
def test_equal():
> assert(1,2,3)==(3,2,1)
E assert (1, 2, 3) == (3, 2, 1)
E At index 0 diff: 1 != 3
E Full diff:
E - (3, 2, 1)
E ? ^ ^
E + (1, 2, 3)
E ? ^ ^
Desktop\123.py:2: AssertionError
====================================== short test summary info ===============================================
FAILED Desktop/123.py::test_equal - assert (1, 2, 3) == (3, 2, 1)
============================================= 1 failed in 0.20s ==============================================
配置Pycharm
解除安裝Pytest
C:\Users\Administrator>pip uninstall pytest
Found existing installation: pytest 5.4.1
Uninstalling pytest-5.4.1:
Would remove:
c:\program files\python38\lib\site-packages\_pytest\*
c:\program files\python38\lib\site-packages\pytest-5.4.1.dist-info\*
c:\program files\python38\lib\site-packages\pytest\*
c:\program files\python38\scripts\py.test.exe
c:\program files\python38\scripts\pytest.exe
Proceed (y/n)? y
Successfully uninstalled pytest-5.4.1
C:\Users\Administrator>