欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytest測試框架基本使用方法詳解

 更新時間:2020年11月25日 11:22:20   作者:-零  
這篇文章主要介紹了Pytest測試框架基本使用方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

pytest介紹

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

1、簡單靈活,容易上手,文檔豐富;

2、支持參數化,可以細粒度地控制要測試的測試用例;

3、能夠支持簡單的單元測試和復雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests);

4、pytest具有很多第三方插件,并且可以自定義擴展

  • 如pytest-selenium(集成selenium)、
  • pytest-html(完美html測試報告生成)、
  • pytest-rerunfailures(失敗case重復執(zhí)行)、
  • pytest-xdist(多CPU分發(fā))、
  • pytest--ordering(控制測試運行的順序)

5、測試用例的skip和xfail處理;

6、可以很好的和CI工具結合,例如jenkins

編寫規(guī)則:

  • 測試文件以test_開頭(以_test結尾也可以)
  • 測試類以Test開頭,并且不能帶有 init 方法
  • 測試函數以test_開頭

斷言使用基本的assert即可

快速示例

test_pyexample.py

import pytest

class TestClass:
    def test_one(self):
      x = "this"
      assert 'h' in x

    def test_two(self):
      x = "hello"
      assert hasattr(x, 'check')

    def test_three(self):
      a = "hello"
      b = "hello world"
      assert a in b

通過命令行運行:

1、cd 到代碼所在的目錄,執(zhí)行命令:py.test test_pyexample.py

2、安裝pytest-sugar插件可以看到進度條

Pycharm配置運行:

1.file->Setting->Tools->Python Integrated Tools->項目名稱->Default test runner->選擇py.test

import pytest

class TestClass:
    def test_one(self):
      x = "this"
      assert 'h' in x

    def test_two(self):
      x = "hello"
      assert hasattr(x, 'check')

    def test_three(self):
      a = "hello"
      b = "hello world"
      assert a in b

if __name__ == "__main__":
  pytest.main('-q test_class.py')

Console常用參數介紹:

  • -v 用于顯示每個測試函數的執(zhí)行結果
  • -q 只顯示整體測試結果
  • -s 用于顯示測試函數中print()函數輸出
  • -x, --exitfirst, exit instantly on first error or failed test
  • -m 只運行帶有裝飾器配置的測試用例
  • -h 幫助
py.test # run all tests below current dir
py.test test_mod.py # run tests in module file test_mod.py
py.test somepath # run all tests below somepath like ./tests/
py.test -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
py.test 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參數化

使用裝飾器:@pytest.mark.parametrize()

單個參數:

import pytest
import random
@pytest.mark.parametrize('x',[(1),(2),(6)])
def test_add(x):
  print(x)
  assert x==random.randrange(1,7)

多個參數:

import pytest
@pytest.mark.parametrize('x,y',[
  (1+2,3),
  (2-0,1),
  (6*2,12),
  (10*2,3),
  ("test","test"),
])
def test_add(x,y):  #必須與上面保持一致,只能用x,y不能用其他字母
  assert x==y

控制測試運行順序

安裝pytest-ordering

pip install pytest-ordering

借助于裝飾器@pytest.mark.run(order=1)控制測試運行的順序

import pytest
import time
value=0
@pytest.mark.run(order=2) #后執(zhí)行order=2
def test_add2():
  print("I am 2")
  time.sleep(2)
  assert value==10
@pytest.mark.run(order=1)  #先執(zhí)行order=1
def test_add():
  print("I am add")
  global value
  value=10
  assert value==10

運行后生成測試報告(htmlReport)

安裝pytest-html:

pip install -U pytest-html

如何使用:

py.test test_pyexample.py --html=report.html

更詳細的測試報告

安裝 pytest-cov:

pip install pytest-cov

如何使用

py.test --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,則認為失敗

多進程運行

安裝pytest-xdist:

pip install -U pytest-xdist

如何使用:

py.test test_pyexample.py -n NUM

其中NUM填寫并發(fā)的進程數。

重新運行失敗的用例

安裝pytest- rerunfailures:

import random
def add(x,y):
  return x+y
def test_add():
  random_value=random.randint(2,7)
  print('random_value:'+str(random_value))
  assert add(1,3)==random_value

如何使用:

命令:pytest --reruns 重試次數

  比如:pytest --reruns 3  表示:運行失敗的用例可以重新運行3次

命令:pytest --reruns 重試次數 --reruns-delay 次數之間的延時設置(單位:秒)

  比如:pytest --reruns 3 --reruns-delay 5  表示:(譯:瑞軟四、地類)運行失敗的用例可以重新運行3次,第一次和第二次的間隔時間為5秒鐘

另外也可以通過裝飾器的方式配置:

@pytest.mark.flaky(reruns=3, reruns_delay=5)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • pygame游戲之旅 添加碰撞效果的方法

    pygame游戲之旅 添加碰撞效果的方法

    這篇文章主要為大家詳細介紹了pygame游戲之旅的第7篇,教大家如何添加碰撞的效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 使用 PyTorch 實現 MLP 并在 MNIST 數據集上驗證方式

    使用 PyTorch 實現 MLP 并在 MNIST 數據集上驗證方式

    今天小編就為大家分享一篇使用 PyTorch 實現 MLP 并在 MNIST 數據集上驗證方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python3中的bytes和str類型詳解

    Python3中的bytes和str類型詳解

    這篇文章主要介紹了Python3中的bytes和str類型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • Python圖像濾波處理操作示例【基于ImageFilter類】

    Python圖像濾波處理操作示例【基于ImageFilter類】

    這篇文章主要介紹了Python圖像濾波處理操作,結合實例形式分析了Python基于ImageFilter類實現的濾波處理相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • python3.x中安裝web.py步驟方法

    python3.x中安裝web.py步驟方法

    在本篇文章里小編給大家分享的是關于python3.x中安裝web.py步驟方法,需要的朋友們可以學習下。
    2020-06-06
  • Python中實現地圖可視化的方法小結

    Python中實現地圖可視化的方法小結

    Python提供了多個強大的庫,如Folium、Matplotlib、Geopandas等,使得創(chuàng)建漂亮而具有信息量的地圖變得簡單而靈活,本文將詳細介紹如何使用這些庫繪制漂亮的地圖,感興趣的可以了解下
    2023-12-12
  • Python分支結構(switch)操作簡介

    Python分支結構(switch)操作簡介

    這篇文章主要介紹了Python分支結構(switch)操作簡介,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python自動化測試筆試面試題精選

    Python自動化測試筆試面試題精選

    在本篇文章里小編給大家整理的是一篇關于Python自動化測試筆試面試時常見的編程題,需要的朋友們可以學習參考下。
    2020-03-03
  • 多線程爬蟲批量下載pcgame圖片url 保存為xml的實現代碼

    多線程爬蟲批量下載pcgame圖片url 保存為xml的實現代碼

    用Python寫的多線程爬蟲批量下載pcgame的圖片url并保存為xml格式,主要是邏輯代碼,喜歡的朋友可以測試下
    2013-01-01
  • pip install 使用國內鏡像的方法示例

    pip install 使用國內鏡像的方法示例

    這篇文章主要介紹了pip install 使用國內鏡像的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04

最新評論