国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 測試框架 pytest

2019-11-11 05:40:41
字體:
來源:轉載
供稿:網友

pytest 有時也被稱為 py.test,是因為它使用的執行命令是 $ py.test。本文中我們使用 pytest 指代這個測試框架,py.test 特指運行命令。

    較于 nose


這里沒有使用像前三篇一樣(簡介-舉例-discovery-環境)式的分段展開,是因為 pytest 與 nose 的基本用法極其相似。因此只做一個比較就好了。他倆的區別僅在于

調用測試的命令不同,pytest 用的是 $ py.test創建測試環境(setup/teardown)的 api 不同   

下面使用一個例子說明 pytest 的 setup/teardown 使用方式。

some_test.py:

import pytest@pytest.fixture(scope='function')def setup_function(request):    def teardown_function():        PRint("teardown_function called.")    request.addfinalizer(teardown_function)    print('setup_function called.')@pytest.fixture(scope='module')def setup_module(request):    def teardown_module():        print("teardown_module called.")    request.addfinalizer(teardown_module)    print('setup_module called.')def test_1(setup_function):    print('Test_1 called.')def test_2(setup_module):    print('Test_2 called.')def test_3(setup_module):    print('Test_3 called.')

pytest 創建測試環境(fixture)的方式如上例所示,通過顯式指定 scope='' 參數來選擇需要使用的 pytest.fixture 裝飾器。即一個 fixture 函數的類型從你定義它的時候就確定了,這與使用@nose.with_setup() 十分不同。對于 scope='function' 的 fixture 函數,它就是會在測試用例的前后分別調用 setup/teardown。測試用例的參數如def test_1(setup_function) 只負責引用具體的對象,它并不關心對方的作用域是函數級的還是模塊級的。

有效的 scope 參數限于:'function','module','class','session',默認為 function

運行上例:$ py.test some_test.py -s-s 用于顯示 print() 函數

============================= test session starts =============================platform win32 -- Python 3.3.2 -- py-1.4.20 -- pytest-2.5.2collected 3 itemstest.py setup_function called.Test_1 called..teardown_function called.setup_module called.Test_2 called..Test_3 called..teardown_module called.========================== 3 passed in 0.02 seconds ===========================

這里需要注意的地方是:setup_module 被調用的位置。

pytest 與 nose 二選一


    首先,單是從不需要使用特定類模板的角度上,nose 和 pytest 就較于 unittest 好出太多了。doctest 比較奇葩我們在這里不比。因此對于 “選一個自己喜歡的測試框架來用” 的問題,就變成了 nose 和 pytest 二選一的問題。

pythontesting.net 的作者非常喜歡 pytest,并表示

pytest 賽高,不服 solo

好吧,其實他說的是 “如果你挑不出 pytest 的毛病,就用這個吧”。

于是下面我們就來挑挑 pytest 的毛病:

它的 setup/teardown 語法與 unittest 的兼容性不如 nose 高,實現方式也不如 nose 直觀    第一條足矣   

畢竟 unittest 還是 Python 自帶的單元測試框架,肯定有很多怕麻煩的人在用,所以與其語法保持一定兼容性能避免很多麻煩。即使 pytest 在命令行中有彩色輸出讓我很喜歡,但這還是不如第一條重要。

實際上,PyPI 中 nose 的下載量也是 pytest 的 8 倍多。

所以假如再繼續寫某一個框架的詳解的話,大概我會選 nose 吧。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尤溪县| 建湖县| 松原市| 灌云县| 财经| 嵩明县| 永城市| 盐源县| 荣昌县| 乌鲁木齐县| 红原县| 新竹市| 崇左市| 屯门区| 綦江县| 肇源县| 屏边| 威远县| 玛多县| 上蔡县| 蒙自县| 瑞丽市| 乌鲁木齐县| 宁德市| 镇雄县| 澄迈县| 望城县| 昌宁县| 广安市| 西昌市| 垦利县| 江山市| 微博| 湘乡市| 额敏县| 本溪| 杭锦后旗| 乌拉特中旗| 宿州市| 肥西县| 靖江市|