佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > pytest之assert斷言的具體使用

pytest之assert斷言的具體使用

熱門標簽:企業(yè)做大做強 電話運營中心 呼叫中心市場需求 百度AI接口 硅谷的囚徒呼叫中心 Win7旗艦版 客戶服務 語音系統(tǒng)

背景

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

說明

本文將從以下幾點做總結:

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

為測試結果作斷言

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

python assert的概念:

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

我們可以在在assert后面添加任何符合python標準的表達式,如果表達式的值通過bool轉換后等于False,則意味著斷言結果為失敗。

以下舉例常用的表達式:

# ./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

'''
# 以上全是合法的表達式且表達式的值都為True,所以測試結果為通過
============================= 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]

'''

為斷言不通過的結果添加說明信息

在編寫測試時,為了提高易用性,我們想知道斷言失敗時的一些關于失敗的原因等說明信息,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應該等于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應該等于6"
E    AssertionError: 3+3應該等于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]
'''

為預期異常作斷言

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

使用示例:

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


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

class TestFunc:

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

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

接下來看看沒有爆出預期異常的示例:

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

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

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

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

見示例:

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

class TestFunc:

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

# 異常測試用例,期望結果為爆出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'>
數據類型錯誤
[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關鍵字參數來完成E.value的斷言,這里運用到的是python中正則表達式的原理。

示例:

該示例意味斷言通過

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

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

# 異常測試用例,期望結果為爆出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 '數據類型錯誤'

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

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

示例:

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

'''
class TestFunc:

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

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

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

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

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函數后:

# ./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函數,這些函數的作用就是用來改變pytest的運行方式和運行效果。所以編寫第三方插件一般是使用這些hook函數。

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

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

標簽:濟南 山西 山西 喀什 海南 長沙 安康 崇左

巨人網絡通訊聲明:本文標題《pytest之assert斷言的具體使用》,本文關鍵詞  ;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    安义县| 武乡县| 贵阳市| 博爱县| 东明县| 阳江市| 伊川县| 南城县| 昆山市| 崇州市| 新巴尔虎右旗| 新巴尔虎左旗| 瓦房店市| 周至县| 靖宇县| 麻江县| 南陵县| 澄迈县| 四子王旗| 泰兴市| 马山县| SHOW| 平山县| 丰县| 壶关县| 唐海县| 阳高县| 临海市| 南宫市| 灵石县| 福清市| 兴海县| 册亨县| 鱼台县| 永泰县| 布尔津县| 沛县| 温州市| 保山市| 开鲁县| 磴口县|