婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > pytest之a(chǎn)ssert斷言的具體使用

pytest之a(chǎn)ssert斷言的具體使用

熱門標(biāo)簽:房產(chǎn)電銷外呼系統(tǒng) 地圖制圖標(biāo)注位置改變是移位嗎 315電話機(jī)器人廣告 浙江電銷卡外呼系統(tǒng)好用嗎 蓋州市地圖標(biāo)注 地圖標(biāo)注微信發(fā)送位置不顯示 南京銷售外呼系統(tǒng)軟件 上海機(jī)器人外呼系統(tǒng)哪家好 地圖標(biāo)注的意義點(diǎn)

背景

本文總結(jié)使用pytest編寫自動(dòng)化測(cè)試時(shí)常用的assert斷言。

說(shuō)明

本文將從以下幾點(diǎn)做總結(jié):

  1. 為測(cè)試結(jié)果作斷言
  2. 為斷言不通過的結(jié)果添加說(shuō)明信息
  3. 為預(yù)期異常作斷言
  4. 為失敗斷言自定義說(shuō)明信息

為測(cè)試結(jié)果作斷言

在斷言方面,pytest框架比其他類似的框架(比如unittest)更加簡(jiǎn)潔,易用,我想這是我選擇pytest作為自動(dòng)化測(cè)試框架之一的原因之一。
pytest的assert斷言關(guān)鍵字支持使用python內(nèi)置的assert表達(dá)式。可以理解為pytest的斷言就是直接使用python自帶的assert關(guān)鍵字。

python assert的概念:

Python assert(斷言)用于判斷一個(gè)表達(dá)式,在表達(dá)式條件為 false 的時(shí)候觸發(fā)異常。

我們可以在在assert后面添加任何符合python標(biāo)準(zhǔn)的表達(dá)式,如果表達(dá)式的值通過bool轉(zhuǎn)換后等于False,則意味著斷言結(jié)果為失敗。

以下舉例常用的表達(dá)式:

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:
 
 def test_add_by_class(self):
  assert add(2,3) == 5

def test_add_by_func_aaa():

 assert 'a' in 'abc'
 assert 'a' not in 'bbc'
 something = True
 assert something
 something = False
 assert not something
 assert 1==1
 assert 1!=2
 assert 'a' is 'a'
 assert 'a' is not 'b'
 assert 1  2
 assert 2 > 1
 assert 1 = 1
 assert 1 >= 1
 assert add(3,3) == 6

'''
# 以上全是合法的表達(dá)式且表達(dá)式的值都為True,所以測(cè)試結(jié)果為通過
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::TestFunc::test_add_by_class PASSED               [ 50%]
test_case/test_func.py::test_add_by_func_aaa PASSED                      [100%]

============================== 2 passed in 0.06s ==============================
[Finished in 1.8s]

'''

為斷言不通過的結(jié)果添加說(shuō)明信息

在編寫測(cè)試時(shí),為了提高易用性,我們想知道斷言失敗時(shí)的一些關(guān)于失敗的原因等說(shuō)明信息,assert也能滿足該功能。
請(qǐng)看示例:

# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:
 def test_add_by_class(self):
  assert add(2,3) == 5


def test_add_by_func_aaa():
 assert add(3,3) == 5, "3+3應(yīng)該等于6"

'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::TestFunc::test_add_by_class PASSED               [ 50%]
test_case/test_func.py::test_add_by_func_aaa FAILED                      [100%]

================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________

    def test_add_by_func_aaa():
    
>    assert add(3,3) == 5, "3+3應(yīng)該等于6"
E    AssertionError: 3+3應(yīng)該等于6
E    assert 6 == 5
E      -6
E      +5

test_case\test_func.py:14: AssertionError
========================= 1 failed, 1 passed in 0.09s =========================
[Finished in 1.4s]
'''

為預(yù)期異常作斷言

在某些測(cè)試用例中,比如異常測(cè)試用例,測(cè)試的結(jié)果必然是失敗并應(yīng)該爆出異常的。這時(shí)候自動(dòng)化測(cè)試用例的期望結(jié)果就是該異常。如果期望結(jié)果等于該異常,那么測(cè)試用例執(zhí)行通過,否則用例結(jié)果為失敗。pytest提供為為預(yù)期異常作斷言的方法:pytest.raises()。一般結(jié)合with上下文管理器使用。

使用示例:

# ./func.py
def add(a,b):
 if isinstance(a,int) and isinstance(b,int):
  return a+b
 else:
  raise NameError('數(shù)據(jù)類型錯(cuò)誤')


# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 # 正常測(cè)試用例
 def test_add_by_class(self):
  assert add(2,3) == 5

# 異常測(cè)試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises(TypeError):
  add('3',4)
  

# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v'])

'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::TestFunc::test_add_by_class PASSED               [ 50%]
test_case/test_func.py::test_add_by_func_aaa PASSED                      [100%]

============================== 2 passed in 0.06s ==============================
[Finished in 1.4s]
''' 

接下來(lái)看看沒有爆出預(yù)期異常的示例:

# ./func.py
def add(a,b):
 # 指定異常
 raise NameError("天降異常")
 if isinstance(a,int) and isinstance(b,int):
  return a+b
 else:
  raise NameError('數(shù)據(jù)類型錯(cuò)誤')

# ./test_case/test_func.py
import pytest
from func import *
'''
class TestFunc:

 # 正常測(cè)試用例
 def test_add_by_class(self):
  assert add(2,3) == 5
'''
# 異常測(cè)試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises(TypeError):
  add('3',4)
  
# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v'])


'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 1 item

test_case/test_func.py::test_add_by_func_aaa FAILED                      [100%]

================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________

    def test_add_by_func_aaa():
     with pytest.raises(TypeError):
>     add('3',4)

test_case\test_func.py:14: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

a = '3', b = 4

    def add(a,b):
     # 指定異常
>    raise NameError("天降異常")
E    NameError: 天降異常

func.py:4: NameError
============================== 1 failed in 0.09s ==============================
[Finished in 1.4s]
'''

判定用例執(zhí)行結(jié)果為失敗。

上面我們只是斷言了異常的類型。但有的時(shí)候我們想更進(jìn)一步斷言異常的說(shuō)明信息,pytest也可以做到。with pytest.raises()執(zhí)行結(jié)束后會(huì)生成一個(gè)ExceptionInfo的實(shí)例對(duì)象。該對(duì)象包含type , value, traceback屬性。value屬性就是我們需要的異常說(shuō)明信息。

見示例:

# ./func.py
def add(a,b):
 if isinstance(a,int) and isinstance(b,int):
  return a+b
 else:
  raise TypeError('數(shù)據(jù)類型錯(cuò)誤')
 
# ./test_case/test_func.py
import pytest
from func import *

class TestFunc:

 # 正常測(cè)試用例
 def test_add_by_class(self):
  assert add(2,3) == 5

# 異常測(cè)試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises(TypeError) as E:
  add('3',4)
 print(E.type)
 print(E.value)
 print(E.traceback)
 # 加入該不通過斷言為了查看stdout
 assert 1 == 2


# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v'])

'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 2 items

test_case/test_func.py::TestFunc::test_add_by_class PASSED               [ 50%]
test_case/test_func.py::test_add_by_func_aaa FAILED                      [100%]

================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________

    def test_add_by_func_aaa():
     with pytest.raises(TypeError) as E:
      add('3',4)
     print(E.type)
     print(E.value)
     print(E.traceback)
>    assert 1 == 2
E    assert 1 == 2
E      -1
E      +2

test_case\test_func.py:18: AssertionError
---------------------------- Captured stdout call -----------------------------
class 'TypeError'>
數(shù)據(jù)類型錯(cuò)誤
[TracebackEntry D:\Python3.7\project\pytest\test_case\test_func.py:14>, TracebackEntry D:\Python3.7\project\pytest\func.py:6>]
========================= 1 failed, 1 passed in 0.10s =========================
[Finished in 1.4s]
'''

控制臺(tái)輸出的“Captured stdout call”就是異常的信息,包含類型,異常說(shuō)明,異常跟蹤信息。
可以通過assert斷言這些信息。

也可以通過給pytest.raises()傳入match關(guān)鍵字參數(shù)來(lái)完成E.value的斷言,這里運(yùn)用到的是python中正則表達(dá)式的原理。

示例:

該示例意味斷言通過

def test_add_by_func_aaa():
 with pytest.raises(TypeError, match=r'.*類型錯(cuò)誤$') as E:
  add('3',4)

該示例意味斷言失敗:

# 異常測(cè)試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises(TypeError, match=r'.*正確$') as E:
  add('3',4)
'''
During handling of the above exception, another exception occurred:

    def test_add_by_func_aaa():
     with pytest.raises(TypeError, match=r'.*正確$') as E:
>     add('3',4)
E     AssertionError: Pattern '.*正確$' not found in '數(shù)據(jù)類型錯(cuò)誤'

test_case\test_func.py:14: AssertionError
'''

如果,某個(gè)測(cè)試用例可能出現(xiàn)不同的預(yù)期異常,只要爆出的異常在預(yù)期的幾個(gè)異常之內(nèi),那么如何斷言呢。解決方法很簡(jiǎn)單,原理和接口都沒變,只是在pytest.raises()中傳入異常類型的參數(shù),從傳入一個(gè)異常類型,改變?yōu)閭魅胍粋€(gè)異常類型組成的元組。同樣只是傳入一個(gè)參數(shù)。

示例:

# ./func.py
def add(a,b):
 raise NameError('名字錯(cuò)了')
 if isinstance(a,int) and isinstance(b,int):
  return a+b
 else:
  raise TypeError('數(shù)據(jù)類型錯(cuò)誤')
 
# ./test_case/test_func.py
import pytest
from func import *

'''
class TestFunc:

 # 正常測(cè)試用例
 def test_add_by_class(self):
  assert add(2,3) == 5
'''

# 異常測(cè)試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises((TypeError,NameError),match=r'.*錯(cuò).*$') as E:
  add('3',4)
 
 
# ./run_test.py
import pytest

if __name__ == '__main__':
 pytest.main(['-v'])
 
'''
============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-5.3.4, py-1.8.1, pluggy-0.13.1 -- D:\Python3.7\python.exe
cachedir: .pytest_cache
rootdir: D:\Python3.7\project\pytest, inifile: pytest.ini
plugins: allure-pytest-2.8.9, rerunfailures-8.0
collecting ... collected 1 item

test_case/test_func.py::test_add_by_func_aaa PASSED                      [100%]

============================== 1 passed in 0.04s ==============================
[Finished in 1.4s]
'''

為失敗斷言自定義說(shuō)明信息

這種行為,相當(dāng)于改變了pytest的運(yùn)行方式,雖然只是一種錦上添花的改變。我們通過編寫hook函數(shù)來(lái)改變pytest的行為。hook函數(shù)是pytest提供的,有很多,各個(gè)hook函數(shù)的詳細(xì)定義應(yīng)該參考pytest的官方文檔。
為失敗斷言自定義說(shuō)明信息是通過pytest_assertrepr_compare這個(gè)hook函數(shù)完成的。
先看沒有編寫pytest_assertrepr_compare這個(gè)hook函數(shù)時(shí),默認(rèn)的失敗斷言說(shuō)明:

def test_add_by_func_aaa():
 assert 'aaa' == 'bbb'

'''
================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________

    def test_add_by_func_aaa():
>    assert 'aaa' == 'bbb'
E    AssertionError: assert 'aaa' == 'bbb'
E      - aaa
E      + bbb

test_case\test_func.py:16: AssertionError
'''

再看編寫pytest_assertrepr_compare這個(gè)hook函數(shù)后:

# ./conftest.py

def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, str) and isinstance(right, str) and op == "==":
        return ['兩個(gè)字符串比較:',
                '   值: %s != %s' % (left, right)]


# ./test_case/test_func.py
import pytest
def test_add_by_func_aaa():
 assert 'aaa' == 'bbb'


'''
.F                                                                       [100%]
================================== FAILURES ===================================
____________________________ test_add_by_func_aaa _____________________________

    def test_add_by_func_aaa():
>    assert 'aaa' == 'bbb'
E    assert 兩個(gè)字符串比較:
E         值: aaa != bbb

test_case\test_func.py:15: AssertionError
1 failed, 1 passed in 0.09s
[Finished in 1.5s]
'''

pytest還提供其他的hook函數(shù),這些函數(shù)的作用就是用來(lái)改變pytest的運(yùn)行方式和運(yùn)行效果。所以編寫第三方插件一般是使用這些hook函數(shù)。

到此這篇關(guān)于pytest之a(chǎn)ssert斷言的具體使用的文章就介紹到這了,更多相關(guān)pytest assert斷言內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 簡(jiǎn)單了解Java斷言利器AssertJ原理及用法
  • Node.js API詳解之 assert模塊用法實(shí)例分析
  • python 錯(cuò)誤處理 assert詳解
  • Python3 assert斷言實(shí)現(xiàn)原理解析
  • 解決pytorch報(bào)錯(cuò):AssertionError: Invalid device id的問題
  • Python assert關(guān)鍵字原理及實(shí)例解析
  • python3 assert 斷言的使用詳解 (區(qū)別于python2)
  • Java Assert.assertEquals案例詳解

標(biāo)簽:雙鴨山 臨汾 陽(yáng)泉 日照 金華 貴州 赤峰 克拉瑪依

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytest之a(chǎn)ssert斷言的具體使用》,本文關(guān)鍵詞  pytest,之,assert,斷言,的,具體,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytest之a(chǎn)ssert斷言的具體使用》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pytest之a(chǎn)ssert斷言的具體使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av
    中文字幕 久热精品 视频在线| 欧美高清视频www夜色资源网| 色综合天天性综合| 欧美一区二区网站| 偷偷要91色婷婷| 91麻豆国产在线观看| 国产欧美日产一区| 国产综合久久久久影院| 国产日韩欧美在线一区| 日韩一级大片在线| 日韩一区二区精品在线观看| 亚洲男人的天堂一区二区| 国产人久久人人人人爽| 国产乱淫av一区二区三区| 蜜桃久久久久久久| 亚洲欧美自拍偷拍| 久久精品视频一区二区| 91精品欧美一区二区三区综合在| 91视频精品在这里| 国产99久久精品| 国产一区二区三区免费观看| 久久成人18免费观看| 天天色图综合网| 婷婷久久综合九色国产成人| 亚洲成av人**亚洲成av**| 亚洲人成人一区二区在线观看| 国产精品久久久久9999吃药| 中文字幕精品一区| 中国色在线观看另类| 国产精品的网站| 国产精品九色蝌蚪自拍| 亚洲色图.com| 一区二区三区自拍| 欧美aaaaaa午夜精品| 亚洲风情在线资源站| 亚洲一区二区三区美女| 一区二区视频在线| 亚洲欧美国产三级| 亚洲午夜久久久久久久久电影院| 亚洲不卡在线观看| 日本视频中文字幕一区二区三区| 麻豆成人久久精品二区三区小说| 精品一区二区三区在线观看| 国产成人午夜电影网| 91蝌蚪porny九色| 欧美三片在线视频观看| 一本到不卡精品视频在线观看| 99久久久久免费精品国产| 91小视频在线免费看| 日本高清不卡视频| 欧美伦理视频网站| 国产丝袜美腿一区二区三区| 日韩伦理免费电影| 午夜私人影院久久久久| 激情综合网天天干| 91亚洲国产成人精品一区二三| 欧美日韩亚洲国产综合| 精品欧美乱码久久久久久| 亚洲欧美怡红院| 日韩va欧美va亚洲va久久| 捆绑变态av一区二区三区| 99久久99久久久精品齐齐| 91精品国产综合久久久蜜臀图片| 久久免费国产精品| 亚洲丰满少妇videoshd| 国产馆精品极品| 欧美电影一区二区三区| 久久久久久97三级| 一级做a爱片久久| 国产精品一区专区| 欧美一区二区在线播放| 中文字幕一区二区三区在线不卡| 奇米影视一区二区三区| 99精品国产99久久久久久白柏| 91精品国产综合久久久久久漫画| 中文字幕在线不卡一区| 国内成+人亚洲+欧美+综合在线 | 中文子幕无线码一区tr| 婷婷开心激情综合| 色哟哟国产精品| 精品对白一区国产伦| 亚洲永久免费视频| 色婷婷久久久综合中文字幕| 久久久久久久久一| 理论电影国产精品| 日韩精品一区二| 亚洲成av人片在线观看| 91片在线免费观看| 中文字幕视频一区| 成人99免费视频| 国产精品毛片a∨一区二区三区| 久久99这里只有精品| 欧美成人国产一区二区| 男女男精品视频网| 99久久夜色精品国产网站| 久久精品一区二区三区不卡牛牛| 奇米色一区二区| 4438亚洲最大| 日韩av网站在线观看| 欧美日韩国产首页| 日韩高清电影一区| 欧美性生活久久| 日韩成人免费在线| 日韩一区二区三区观看| 男女性色大片免费观看一区二区 | 91九色最新地址| 亚洲一区视频在线| 91 com成人网| 国内精品伊人久久久久影院对白| 精品sm在线观看| 国产mv日韩mv欧美| 国产精品卡一卡二| 色菇凉天天综合网| 国产成人精品一区二| 日本一区二区三区久久久久久久久不 | 欧美系列一区二区| 视频一区二区不卡| 欧美一区二区视频在线观看2020 | 久久亚洲精华国产精华液| 国产91露脸合集magnet| 国产欧美日韩三区| 不卡的av网站| 亚洲第一会所有码转帖| 日韩一区二区影院| 成人午夜看片网址| 亚洲一区视频在线| 欧美电影免费提供在线观看| 激情文学综合插| 最新国产精品久久精品| 欧美欧美午夜aⅴ在线观看| 六月丁香综合在线视频| 国产精品久久久久永久免费观看| 在线一区二区三区做爰视频网站| 五月婷婷综合在线| 国产欧美日本一区二区三区| 欧洲色大大久久| 亚洲444eee在线观看| 久久久国产精华| 色呦呦网站一区| 韩国视频一区二区| 亚洲最新视频在线播放| 91精品国产综合久久精品麻豆| 国产剧情一区在线| 午夜视频在线观看一区二区| 国产日韩欧美精品电影三级在线 | 另类小说一区二区三区| 国产精品 欧美精品| 欧美视频一区二区三区在线观看| 亚洲成av人片一区二区梦乃| 91精品国产aⅴ一区二区| 亚洲18女电影在线观看| 久久精品免视看| 国产一区二区视频在线| 国产欧美视频一区二区三区| 666欧美在线视频| 欧美亚洲国产一区在线观看网站| 国内精品第一页| 免费成人美女在线观看| 亚洲人亚洲人成电影网站色| 日韩一区二区三区四区| 亚洲色图一区二区| 日本高清视频一区二区| 国产精品羞羞答答xxdd| 久久精品国产99久久6| 亚洲夂夂婷婷色拍ww47| 亚洲精品视频一区二区| 337p日本欧洲亚洲大胆精品| 欧美最新大片在线看 | 99久久精品99国产精品| 韩国精品免费视频| 人人狠狠综合久久亚洲| 亚洲久草在线视频| 亚洲欧洲韩国日本视频 | 亚洲精品成a人| 国产日韩欧美精品一区| 久久精品亚洲精品国产欧美kt∨| 欧美一区二区三区四区在线观看| 色婷婷亚洲精品| 99久久精品免费精品国产| av激情亚洲男人天堂| 99久久精品国产导航| 欧美综合视频在线观看| 欧美在线观看一二区| 欧美午夜精品理论片a级按摩| 欧美午夜不卡在线观看免费| 7777精品伊人久久久大香线蕉 | 久久婷婷国产综合精品青草| 久久久精品国产99久久精品芒果| 久久久久久毛片| 亚洲视频中文字幕| 亚洲专区一二三| 精久久久久久久久久久| 成人免费看视频| 色欧美片视频在线观看| 91 com成人网| 国产日韩欧美综合在线| 亚洲一区二区视频在线观看| 青青草精品视频| 国产一区二区三区国产| 99久久99久久精品免费看蜜桃|