此文已由做者吳琪惠受權網易雲社區發佈。
html
歡迎訪問網易雲社區,瞭解更多網易技術產品運營經驗。python
純官網譯文而已。。。
linux
pytest是一個成熟的、全功能的python測試工具。json
pytest框架編寫測試用例時,小的用例會變得更容易編寫,但對於複雜的應用或者庫應該更謹慎選擇。session
特徵:架構
1.斷言失敗以後具有詳細的失敗信息(不無需記住self.asseer*的名字)app
2.自動失敗測試模塊和方法框架
3.模塊化組件,可用於管理小的或者參數化長時間存活的測試資源ide
4.能夠在box外運行uniitest和nose測試組件模塊化
5.支持Python2.6+, Python3.3+, PyPy-2.3, Jython-2.5 (未測試)
6.豐富的插件架構,擁有超過150+個外部插件和人氣活動的論壇社區
安裝:
支持的python版本:Python 2.6,2.7,3.3,3.4,3.5, Jython, PyPy-2.3
支持的平臺:Unix/Posix ,Windows
Pypi鏈接:pytest
安裝命令:
pip install -U pytest
檢查安裝結果:
$ pytest --version This is pytest version 3.0.6, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py
第一次運行一個簡單的例子:
# content of test_sample.pydef inc(x): return x + 1def test_answer(): assert inc(3) == 5
運行結果:
$ pytest ======= test session starts ======== platform linux -- Python 3.5.2, pytest-3.0.6, py-1.4.33, pluggy-0.4.0rootdir: $REGENDOC_TMPDIR, inifile: collected 1 items test_sample.py F ======= FAILURES ======== _______ test_answer ________ def test_answer():> assert inc(3) == 5E assert 4 == 5E + where 4 = inc(3) test_sample.py:5: AssertionError ======= 1 failed in 0.12 seconds ========
上面的例子失敗是由於func(3)不該該返回5,而是4
提示:你能夠簡單的使用 assert 語句來進行測試是否異常,pytest內置了詳盡的斷言,可只能的識別 assert表達式的中間項,你無需記住JUint那麼多的傳統方法
運行多個測試用例:
pytest會運行當前路徑以及其子路徑下的全部格式如 test_*.py 或者 *_test.py文件,一般遵循標準試驗發現規則。
異常斷言:
若是你想要assert一些拋出異常的代碼,你可使用raises,運行腳本,使用「quiet」靜默模式(-q):
# content of test_sysexit.pyimport pytestdef f(): raise SystemExit(1)def test_mytest(): with pytest.raises(SystemExit): f()
運行結果:
$ pytest -q test_sysexit.py .1 passed in 0.12 seconds
在一個類中分組用例:
實際場景中,會遇到一個類中或者一個模塊下有一些邏輯上有關聯的用例組,舉個例子:
# content of test_class.pyclass TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check')
上面兩個用例,沒有子類,所以咱們能夠直接使用文件名來運行:
$ 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 assert FalseE + where False = hasattr('hello', 'check') test_class.py:8: AssertionError1 failed, 1 passed in 0.12 seconds
第一個用例passed,第二個failed,而且咱們能夠狀況的看着斷言中的一些中間值,幫助咱們理解錯誤緣由。
功能測試用例:生成惟一的臨時目錄
功能測試常常須要建立一些文件,並將其傳遞給應用程序對象。pytest提供 Builtin fixtures/function 參數容許請求任意資源,例如惟一的臨時目錄:
# content of test_tmpdir.pydef test_needsfiles(tmpdir): print (tmpdir) assert 0
在函數中打印了參數 tmpdir,pytest會在執行測試方法以前,查找和調用一個fixture factory來建立資源。運行例子:
$ pytest -q test_tmpdir.py F ======= FAILURES ======== _______ test_needsfiles ________ tmpdir = local('PYTEST_TMPDIR/test_needsfiles0') def test_needsfiles(tmpdir): print (tmpdir) > assert 0E assert 0test_tmpdir.py:3: AssertionError --------------------------- Captured stdout call --------------------------- PYTEST_TMPDIR/test_needsfiles01 failed in 0.12 seconds
在測試執行以前,一個 惟一的-單獨的-測試-執行 臨時路徑被建立。
斷言的前面的print內容也會打印出來,測試時能夠多加print語句,保證異常時輸出一些有用的信息。
如下命令能夠看到更多的內置函數:
pytest --fixtures # shows builtin and custom fixtures
E:\0WORKS\MyPytest>py.test --fixtures ============================= test session starts ============================= platform win32 -- Python 2.7.10, pytest-3.0.4, py-1.4.31, pluggy-0.4.0rootdir: E:\0WORKS\MyPytest, inifile:plugins: html-1.11.0, rerunfailures-2.1.0collected 1 items cache Return a cache object that can persist state between testing sessions. cache.get(key, default) cache.set(key, value) Keys must be a ``/`` separated value, where the first part is usually the name of your plugin or application to avoid clashes with other cache users. Values can be any object handled by the json stdlib module. capsys Enable capturing of writes to sys.stdout/sys.stderr and make captured output available via ``capsys.readouterr()`` method calls which return a ``(out, err)`` tuple. capfd Enable capturing of writes to file descriptors 1 and 2 and make captured output available via ``capfd.readouterr()`` method calls which return a ``(out, err)`` tuple. doctest_namespace Inject names into the doctest namespace. pytestconfig the pytest config object with access to command line opts. record_xml_property Add extra xml properties to the tag for the calling test. The fixture is callable with ``(name, value)``, with value being automatical ly xml-encoded. monkeypatch The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: monkeypatch.setattr(obj, name, value, raising=True) monkeypatch.delattr(obj, name, raising=True) monkeypatch.setitem(mapping, name, value) monkeypatch.delitem(obj, name, raising=True) monkeypatch.setenv(name, value, prepend=False) monkeypatch.delenv(name, value, raising=True) monkeypatch.syspath_prepend(path) monkeypatch.chdir(path) All modifications will be undone after the requesting test function or fixture has finished. The ``raising`` parameter determines if a KeyError or AttributeError will be raised if the set/deletion operation has no target. recwarn Return a WarningsRecorder instance that provides these methods: * ``pop(category=None)``: return last warning matching the category. * ``clear()``: clear list of warnings See http://docs.python.org/library/warnings.html for information on warning categories. tmpdir_factory Return a TempdirFactory instance for the test session. tmpdir Return a temporary directory path object which is unique to each test function invocation, created as a sub directory of the base temporary directory. The returned object is a `py.path.local`_ path object. ------------------ fixtures defined from pytest_html.plugin ------------------- environment Provide environment details for HTML report ======================== no tests ran in 0.21 seconds =========================
網易雲免費體驗館,0成本體驗20+款雲產品!
更多網易技術、產品、運營經驗分享請點擊。
相關文章:
【推薦】 Kubernetes在網易雲中的落地優化實踐