日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Pytest - 使用介绍

發布時間:2023/11/28 生活经验 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytest - 使用介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 概述

pytest是一個非常成熟的全功能的Python測試框架,主要特點有以下幾點:

  • 1、簡單靈活,容易上手,文檔豐富;
  • 2、支持參數化,可以細粒度地控制要測試的測試用例;
  • 3、能夠支持簡單的單元測試和復雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests);
  • 4、pytest具有很多第三方插件,并且可以自定義擴展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報告生成)、pytest-rerunfailures(失敗case重復執行)、pytest-xdist(多CPU分發)等;
  • 5、測試用例的skip和xfail處理;
  • 6、可以很好的和CI工具結合,例如jenkins

2. 使用介紹

2.1. 安裝

pip install pytest

2.2. 示例代碼

編寫規則

編寫pytest測試樣例非常簡單,只需要按照下面的規則:

  • 測試文件以test_開頭(以_test結尾也可以)
  • 測試類以Test開頭,并且不能帶有 init 方法
  • 測試函數以test_開頭
  • 斷言使用基本的assert即可

pytest1.py

# -*- coding:utf-8 -*-
import pytest@pytest.fixture(scope='function')
def setup_function(request): def teardown_function(): print("teardown_function called.") request.addfinalizer(teardown_function) # 此內嵌函數做teardown工作 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.') @pytest.mark.website 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.') assert 2==1+1 # 通過assert斷言確認測試結果是否符合預期 

fixture的scope參數

scope參數有四種,分別是'function','module','class','session',默認為function。

  • function:每個test都運行,默認是function的scope
  • class:每個class的所有test只運行一次
  • module:每個module的所有test只運行一次
  • session:每個session只運行一次

setup和teardown操作

  • setup,在測試函數或類之前執行,完成準備工作,例如數據庫鏈接、測試數據、打開文件等
  • teardown,在測試函數或類之后執行,完成收尾工作,例如斷開數據庫鏈接、回收內存資源等
  • 備注:也可以通過在fixture函數中通過yield實現setup和teardown功能

2.3. 測試結果

如何執行

  • pytest # run all tests below current dir
  • pytest test_mod.py # run tests in module file test_mod.py
  • pytest somepath # run all tests below somepath like ./tests/
  • pytest -k stringexpr # only run tests with names that match the
    # the "string expression", e.g. "MyClass and not method"
    # will select TestMyClass.test_something
    # but not TestMyClass.test_method_simple
  • pytest test_mod.py::test_func # only run tests that match the "node ID",
    # e.g "test_mod.py::test_func" will be selected
    # only run test_func in test_mod.py

通過pytest.mark對test方法分類執行

通過@pytest.mark控制需要執行哪些feature的test,例如在執行test前增加修飾@pytest.mark.website

  • 通過 -m "website" 執行有website標記的test方法
$ pytest  -v -m "website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522925202 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_1 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. =============================================================================== 2 tests deselected =============================================================================== =========================================================== 1 passed, 2 deselected, 1 pytest-warnings in 0.00 seconds ============================================================ 
  • 通過 -m "not website" 執行沒有website標記的test方法
$ pytest  -v -m "not website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522925192 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_3 PASSED pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. =============================================================================== 1 tests deselected =============================================================================== =========================================================== 2 passed, 1 deselected, 1 pytest-warnings in 0.00 seconds ============================================================ 

Console參數介紹

  • -v 用于顯示每個測試函數的執行結果
  • -q 只顯示整體測試結果
  • -s 用于顯示測試函數中print()函數輸出
  • -x, --exitfirst, exit instantly on first error or failed test
  • -h 幫助

Case 1

$ pytest -v pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522920341 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_1 PASSED pytest1.py::test_3 PASSED pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. ================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds =================================================================== 

Case 2

$ pytest -s pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 Using --randomly-seed=1522920508 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py setup_function called. Test_1 called. .teardown_function called. setup_module called. Test_2 called. .Test_3 called. .teardown_module called. ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. ================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds =================================================================== 

3. 擴展插件

3.1. 測試報告

安裝與樣例

pip install pytest-cov # 計算pytest覆蓋率,支持輸出多種格式的測試報告
pytest --cov-report=html --cov=./ test_code_target_dir

Console參數介紹

  • --cov=[path], measure coverage for filesystem path (multi-allowed), 指定被測試對象,用于計算測試覆蓋率
  • --cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 測試報告的類型
  • --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
  • --no-cov-on-fail, do not report coverage if test run fails, default: False,如果測試失敗,不生成測試報告
  • --cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果測試覆蓋率低于MIN,則認為失敗

Console Result

---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ---------------------------------------------------------------- Name Stmts Miss Cover -------------------------------- pytest1.py 18 0 100% 

Html Result

image.png

3.2. 測試順序隨機

pip install pytest-randomly

3.3. 分布式測試

pip install pytest-xdist

3.4. 出錯立即返回

pip install pytest-instafail

4. 參考

  • python的測試工具大全
    https://wiki.python.org/moin/PythonTestingToolsTaxonomy

  • python主流的測試工具橫向比較
    http://docs.python-guide.org/en/latest/writing/tests/
    http://pythontesting.net/test-podcast/

  • python單元測試框架pytest簡介
    https://blog.csdn.net/liuchunming033/article/details/46501653



作者:紅薯愛帥
鏈接:https://www.jianshu.com/p/a754e3d47671
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權并注明出處。

總結

以上是生活随笔為你收集整理的Pytest - 使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久精品色 | 男人与雌性宠物交啪啪 | 体内精视频xxxxx | 91在线播 | 99久久精品国产一区二区成人 | 欧美尹人 | 欧美亚洲免费 | 日本国产视频 | 成人污在线观看 | 婷婷亚洲综合 | 久久久久99精品成人片直播 | 夫妻淫语绿帽对白 | 奇米成人网 | 好男人在线视频www 亚洲福利国产 | 91天天色 | 99精品一区二区三区 | 国产视频精品一区二区三区 | 久久精品aaaaaa毛片 | xxxx96| 麻豆国产精品一区 | 亚洲综合色在线观看 | 成人性生交免费看 | 日韩一级在线视频 | 亚洲色综合| 91日本视频 | 99性趣网| 日韩成人精品 | 黑人一级视频 | 久久久久久久久久久久久女过产乱 | 污片网站| 天天舔天天爽 | 精品热| 亚洲AV无码成人精品区在线观 | 日本色片网站 | 污片免费看 | 中文字幕第一页亚洲 | av成人在线免费观看 | 操大逼网站 | 好大好爽视频 | 日本大尺度吃奶做爰久久久绯色 | 亚洲国产精品久久久久久久 | 伊人激情综合网 | 免费裸体视频女性 | 男女在线观看 | 久久伊人爱 | 女同性恋毛片 | 男人的天堂在线视频 | 捆绑最紧bdsm视频 | 久久九精品 | 凹凸视频一区二区 | 国产在线一卡二卡 | 一区二区国产精品精华液 | 少妇特黄a一区二区三区88av | 日韩色小说 | 中文字幕一区二区三区精华液 | 欧美激情视频二区 | 日韩中文字幕一区 | 激情内射亚洲一区二区三区爱妻 | 男生裸体视频 | 国产精品7777777 | 韩国禁欲系高级感电影 | 欧美 日韩 国产 成人 在线观看 | 欧美午夜精品久久久久久浪潮 | 午夜激情在线观看 | 国产视频你懂的 | 亚洲欧美一区二区在线观看 | 献给魔王伊伏洛基亚吧动漫在线观看 | 久久亚洲av午夜福利精品一区 | 国产三级一区二区三区 | 风间由美在线视频 | 国产精品视频一二区 | 国产精品久久久久久久久久久久久 | 欧美gv在线观看 | www.激情网 | av鲁丝一区鲁丝二区鲁丝 | 美女诱惑一区 | 成人激情av | 国产又粗又黄的视频 | 伊人网五月天 | 牛av在线| 99久免费精品视频在线观78 | 日本黄xxxxxxxxx100 | 激情综合网激情 | 成人综合网址 | 91久久国产综合久久91 | 在线免费看污片 | 一区二区三区在线视频免费观看 | 日韩成人在线播放 | 日韩在观看线 | 欧美日韩观看 | 嫩草视频在线观看视频 | 国产视频精选 | 国产毛片视频网站 | 久久精品视频一区 | 男女视频一区 | 免费色网址 | av免费大全 | 五月激情五月婷婷 | 久久久91精品 |