天天看點

[翻譯]pytest測試架構(一)

此文已由作者吳琪惠授權網易雲社群釋出。

歡迎通路網易雲社群,了解更多網易技術産品營運經驗。

純官網譯文而已。。。

pytest是一個成熟的、全功能的python測試工具。

pytest架構編寫測試用例時,小的用例會變得更容易編寫,但對于複雜的應用或者庫應該更謹慎選擇。

特征:

1.斷言失敗之後具備詳細的失敗資訊(不無需記住self.asseer*的名字)

2.自動失敗測試子產品和方法

3.子產品化元件,可用于管理小的或者參數化長時間存活的測試資源

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在網易雲中的落地優化實踐

繼續閱讀