最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年04月16日 08:34:02   作者:阿刁阿  
這篇文章主要介紹了pytest之a(chǎn)ssert斷言的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

背景

本文總結(jié)使用pytest編寫自動化測試時常用的assert斷言。

說明

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

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

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

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

python assert的概念:

Python assert(斷言)用于判斷一個表達(dá)式,在表達(dá)式條件為 false 的時候觸發(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,所以測試結(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é)果添加說明信息

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

# ./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ù)期異常作斷言

在某些測試用例中,比如異常測試用例,測試的結(jié)果必然是失敗并應(yīng)該爆出異常的。這時候自動化測試用例的期望結(jié)果就是該異常。如果期望結(jié)果等于該異常,那么測試用例執(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ù)類型錯誤')


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

class TestFunc:

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

# 異常測試用例,期望結(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]
''' 

接下來看看沒有爆出預(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ù)類型錯誤')

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

 # 正常測試用例
 def test_add_by_class(self):
  assert add(2,3) == 5
'''
# 異常測試用例,期望結(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é)果為失敗。

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

見示例:

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

class TestFunc:

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

# 異常測試用例,期望結(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ù)類型錯誤
[<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]
'''

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

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

示例:

該示例意味斷言通過

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

該示例意味斷言失?。?/p>

# 異常測試用例,期望結(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ù)類型錯誤'

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

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

示例:

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

'''
class TestFunc:

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

# 異常測試用例,期望結(jié)果為爆出TypeError異常
def test_add_by_func_aaa():
 with pytest.raises((TypeError,NameError),match=r'.*錯.*$') 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]
'''

為失敗斷言自定義說明信息

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

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這個hook函數(shù)后:

# ./conftest.py

def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, str) and isinstance(right, str) and op == "==":
        return ['兩個字符串比較:',
                '   值: %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 兩個字符串比較:
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ù)的作用就是用來改變pytest的運(yùn)行方式和運(yùn)行效果。所以編寫第三方插件一般是使用這些hook函數(shù)。

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

相關(guān)文章

最新評論

阿尔山市| 柳河县| 明光市| 抚顺市| 沂源县| 台南县| 新宁县| 广平县| 府谷县| 武威市| 蓬莱市| 山西省| 花莲市| 象山县| 济阳县| 伽师县| 新余市| 谷城县| 宜川县| 海淀区| 宁陵县| 石棉县| 大理市| 库尔勒市| 翼城县| 卫辉市| 阳东县| 五家渠市| 喀喇沁旗| 广饶县| 临城县| 永寿县| 苍山县| 盖州市| 中西区| 颍上县| 互助| 建始县| 彭泽县| 岱山县| 元朗区|