1. 程式人生 > >[翻譯]pytest測試框架(一)

[翻譯]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+款雲產品! 

更多網易技術、產品、運營經驗分享請點選




相關文章:
【推薦】 iOS 安裝包瘦身 (上篇)
【推薦】 爬蟲開發python工具包介紹 (2)
【推薦】 使用Phaser開發你的第一個H5遊戲(一)