安裝及入門
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
1.在你的命令行執行以下命令
pip install -U pytest
2.檢查你是否安裝了正確的版本
$ 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
.
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_開頭的方法。測試類無需再繼承任何對象。我們只需要簡單地通過文件名來運行這個模塊。
$ pytest -q test_class.py
.F? ================================= 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
================================= 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處理的更多信息,請參見:臨時目錄和文件
進一步閱讀
1.查看其他pytest文檔資源,來幫助你建立自定義測試用例及獨特的工作流:
2.“使用pytest -m pytest來調用pyest”- 命令行調用示例
3.“將pytest與原有測試套件一起使用”- 使用之前的測試用例
4.“使用屬性標記測試方法”-pytest.mark相關信息
5.“pytest fixtures:顯式,模塊化,可擴展”- 為您的測試提供功能基準
6.“插件編寫”- 管理和編寫插件
7.“優質集成實踐”- 虛擬環境和測試分層
軟件測試Pytest教程? ? ? ? ? ? ? ? ——longtest原創