安裝及入門
Python支持版本: Python 2.6,2.7,3.3,3.4,3.5,Jython,PyPy-2.3
支持的平台: Unix/Posix and Windows
PyPI包名: pytest
依賴項: py,colorama (Windows)
PDF文檔: 下載最新版本文檔
Pytest是一個使創建簡單及可擴展性測試用例變得非常方便的框架。測試用例清晰、易讀而無需大量的繁瑣代碼。只要幾分鍾你就可以對你的應用程序或者庫展開一個小型的單元測試或者復雜的功能測試。
安裝 Pytest
在命令行執行以下命令
pip install -U pytest
檢查安裝的Pytest版本
$ pytest --version
This is pytest version 3.x.y,imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
創建你的第一個測試用例
只需要4行代碼即可創建一個簡單的測試用例:
# test_sample.py文件內容
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
就是這么簡單。現在你可以執行一下這個測試用例:
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-3.x.y,py-1.x.y,pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR,inifile:
collected 1 item
test_sample.py F [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
> assert func(3) == 5
E assert 4 == 5
E + where 4 = func(3)
test_sample.py:5: AssertionError
========================= 1 failed in 0.12 seconds =========================
由於func(3)
並不等於5
,這次測試返回了一個失敗的結果信息。
注意:
你可以使用assert
語句來斷言你測試用例的期望結果。Pytest的高級斷言內省機制, 可以智能地展示斷言表達式的中間結果, 來避免來源於JUnit的方法中的變量名重復問題。
執行多條測試用例
pytest
命令會執行當前目錄及子目錄下所有test_*.py
及*_test.py
格式的文件。一般來說,用例需要遵循標准的測試發現規則。
斷言拋出了指定異常
使用raise
可以在相應代碼的拋出的指定異常:
# test_sysexit.py文件內容
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
使用“靜默”模式,執行這個測試用例如:
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.12 seconds
使用類組織多條測試用例
一旦你需要開發多條測試用例,你可能會想要使用類來組織它們。使用Pytest可以很輕松的創建包含多條用例的測試類:
# test_class.py文件內容
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x,'check')
Pytest
可以發現所有遵循Python測試用例發現約定規則的用例,所以它能找到Test
開頭的測試類外以及類中所有以test_
開頭的函數及方法。測試類無需再繼承任何對象。我們只需要簡單地通過文件名來運行這個模塊即可。
$ pytest -q test_class.py
.F [100%]
================================= FAILURES =================================
____________________________ TestClass.test_two ____________________________
self = <test_class.TestClass object at 0xdeadbeef>
def test_two(self):
x = "hello"
> assert hasattr(x,'check')
E AssertionError: assert False
E + where False = hasattr('hello','check')
test_class.py:8: AssertionError
1 failed,1 passed in 0.12 seconds
第一條用例執行成功,第二天用例執行失敗。你可以很容易地通過斷言中變量的中間值來理解失敗的原因。
函數測試中請求使用獨立的臨時目錄
Pytest
提供了內置fixtures方法參數,來使用任意資源,比如一個獨立的臨時目錄:
# test_tmpdir.py文件內容
def test_needsfiles(tmpdir):
print (tmpdir)
assert 0
在測試用例函數使用tmpdir
作為參數,Pytest將在測試用例函數調用之前查找並調用fixture工廠方法來創建相應的資源。在測試運行之前,Pytest為每個測試用例創建一個獨立的臨時目錄:
$ pytest -q test_tmpdir.py
F [100%]
================================= FAILURES =================================
_____________________________ test_needsfiles ______________________________
tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
def test_needsfiles(tmpdir):
print (tmpdir)
> assert 0
E assert 0
test_tmpdir.py:3: AssertionError
--------------------------- Captured stdout call ---------------------------
PYTEST_TMPDIR/test_needsfiles0
1 failed in 0.12 seconds
有關tmpdir處理的更多信息,請參見: 臨時目錄和文件
進一步閱讀
查看其他pytest文檔資源,來幫助你建立自定義測試用例及獨特的工作流:
- “使用pytest -m pytest來調用pyest” - 命令行調用示例
- “將pytest與原有測試套件一起使用”- 使用之前的測試用例
- “使用屬性標記測試用例” -
pytest.mark
相關信息 - “pytest fixtures:顯式,模塊化,可擴展” - 為你的測試提供函數基准
- “插件編寫” - 管理和編寫插件
- “優質集成實踐” - 虛擬環境和測試分層