目錄:
- 安裝及入門(mén)
- 使用和調(diào)用方法
- 原有TestSuite使用方法
- 斷言的編寫(xiě)和報(bào)告
- Pytest fixtures:清晰 模塊化 易擴(kuò)展
- 使用Marks標(biāo)記測(cè)試用例
- Monkeypatching/對(duì)模塊和環(huán)境進(jìn)行Mock
- 使用tmp目錄和文件
- 捕獲stdout及stderr輸出
- 捕獲警告信息
- 模塊及測(cè)試文件中集成doctest測(cè)試
- skip及xfail: 處理不能成功的測(cè)試用例
- Fixture方法及測(cè)試用例的參數(shù)化
- 緩存: 使用跨執(zhí)行狀態(tài)
- unittest.TestCase支持
- 運(yùn)行Nose用例
- 經(jīng)典xUnit風(fēng)格的setup/teardown
- 安裝和使用插件
- 插件編寫(xiě)
- 編寫(xiě)鉤子(hook)方法
- 運(yùn)行日志
- API參考
- 優(yōu)質(zhì)集成實(shí)踐
- 片狀測(cè)試
- Pytest導(dǎo)入機(jī)制及sys.path/PYTHONPATH
- 配置選項(xiàng)
- 示例及自定義技巧
- Bash自動(dòng)補(bǔ)全設(shè)置
安裝及入門(mén)
Python支持版本: Python 2.6,2.7,3.3,3.4,3.5, Jython, PyPy-2.3
支持的平臺(tái): Unix/Posix and Windows
PyPI包名: pytest
依賴項(xiàng): py, colorama (Windows)
PDF文檔: 下載最新版本文檔
pytest是一個(gè)方便創(chuàng)建簡(jiǎn)單、可擴(kuò)展性測(cè)試用例的框架。測(cè)試用例清晰、易讀而無(wú)需大量的繁瑣代碼。你幾分鐘內(nèi)便可針對(duì)你的應(yīng)用程序或庫(kù)開(kāi)展一個(gè)小型單元測(cè)試或者復(fù)雜功能測(cè)試。
安裝pytest
- 在你的命令行執(zhí)行以下命令
pip install -U pytest
- 檢查你是否安裝了正確的版本
$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
創(chuàng)建你的第一個(gè)測(cè)試用例
使用簡(jiǎn)單的4行代碼創(chuàng)建一個(gè)簡(jiǎn)單的測(cè)試方法:
# test_sample.py文件內(nèi)容
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
就是這樣。你可以執(zhí)行一下這個(gè)測(cè)試方法:
$ 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,這次測(cè)試返回了一個(gè)失敗報(bào)告。
注意:
你可以使用assert語(yǔ)句來(lái)驗(yàn)證你測(cè)試用例的期望結(jié)果。pytest的高級(jí)斷言內(nèi)省機(jī)制可以智能地報(bào)告展示斷言表達(dá)式的中間值來(lái)避免來(lái)源于JUnit的方法中的變量名重復(fù)問(wèn)題。
執(zhí)行多條測(cè)試
pytest會(huì)執(zhí)行當(dāng)前目錄及子目錄下所有test_*.py及*_test.py格式的文件。一般來(lái)說(shuō),它遵循標(biāo)準(zhǔn)的測(cè)試發(fā)現(xiàn)規(guī)則。
斷言指定異常
使用raise可以用于斷言相應(yīng)代碼的拋出的指定異常:
# test_sysexit.py文件內(nèi)容
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
使用“安靜”模式,執(zhí)行這個(gè)測(cè)試方法:
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.12 seconds
使用類來(lái)組織測(cè)試用例
一旦你需要開(kāi)發(fā)多條測(cè)試用例,你可能會(huì)想要使用類來(lái)組織它們。使用pytest可以很輕松的創(chuàng)建包含多條用例的測(cè)試類:
# test_class.py文件內(nèi)容
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
pytest可以發(fā)現(xiàn)所有遵循Python測(cè)試用例發(fā)現(xiàn)約定規(guī)則的用例,所以它能找到類外以及類中所有以test_開(kāi)頭的方法。測(cè)試類無(wú)需再繼承任何對(duì)象。我們只需要簡(jiǎn)單地通過(guò)文件名來(lái)運(yùn)行這個(gè)模塊。
$ 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
第一條用例執(zhí)行成功,第二天用例執(zhí)行失敗。你可以很容易地通過(guò)斷言中變量的中間值來(lái)理解失敗的原因。
功能測(cè)試中請(qǐng)求使用獨(dú)立的臨時(shí)目錄
pytest提供了內(nèi)置fixtures及方法參數(shù)來(lái)請(qǐng)求任意資源,比如一個(gè)獨(dú)立的臨時(shí)目錄:
# test_tmpdir.py文件內(nèi)容
def test_needsfiles(tmpdir):
print (tmpdir)
assert 0
在測(cè)試函數(shù)參數(shù)中使用tmpdir,pytest將在測(cè)試函數(shù)調(diào)用之前查找并調(diào)用fixture工廠方法來(lái)創(chuàng)建資源。在測(cè)試運(yùn)行之前,pytest創(chuàng)建一個(gè)每個(gè)測(cè)試唯一的臨時(shí)目錄:
$ 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
有關(guān)tmpdir處理的更多信息,請(qǐng)參見(jiàn): 臨時(shí)目錄和文件
進(jìn)一步閱讀
查看其他pytest文檔資源,來(lái)幫助你建立自定義測(cè)試用例及獨(dú)特的工作流:
- “使用pytest -m pytest來(lái)調(diào)用pyest” - 命令行調(diào)用示例
- “將pytest與原有測(cè)試套件一起使用”- 使用之前的測(cè)試用例
-
“使用屬性標(biāo)記測(cè)試方法” -
pytest.mark相關(guān)信息 - “pytest fixtures:顯式,模塊化,可擴(kuò)展” - 為您的測(cè)試提供功能基準(zhǔn)
- “插件編寫(xiě)” - 管理和編寫(xiě)插件
- “優(yōu)質(zhì)集成實(shí)踐” - 虛擬環(huán)境和測(cè)試分層