pytest之a(chǎn)ssert斷言的具體使用
背景
本文總結(jié)使用pytest編寫(xiě)自動(dòng)化測(cè)試時(shí)常用的assert斷言。
說(shuō)明
本文將從以下幾點(diǎn)做總結(jié):
- 為測(cè)試結(jié)果作斷言
- 為斷言不通過(guò)的結(jié)果添加說(shuō)明信息
- 為預(yù)期異常作斷言
- 為失敗斷言自定義說(shuō)明信息
為測(cè)試結(jié)果作斷言
在斷言方面,pytest框架比其他類(lèi)似的框架(比如unittest)更加簡(jiǎn)潔,易用,我想這是我選擇pytest作為自動(dòng)化測(cè)試框架之一的原因之一。
pytest的assert斷言關(guān)鍵字支持使用python內(nèi)置的assert表達(dá)式??梢岳斫鉃閜ytest的斷言就是直接使用python自帶的assert關(guān)鍵字。
python assert的概念:
Python assert(斷言)用于判斷一個(gè)表達(dá)式,在表達(dá)式條件為 false 的時(shí)候觸發(fā)異常。
我們可以在在assert后面添加任何符合python標(biāo)準(zhǔn)的表達(dá)式,如果表達(dá)式的值通過(guò)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á)式的值都為T(mén)rue,所以測(cè)試結(jié)果為通過(guò) ============================= 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] '''
為斷言不通過(guò)的結(jié)果添加說(shuō)明信息
在編寫(xiě)測(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í)行通過(guò),否則用例結(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ù)類(lèi)型錯(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)看看沒(mé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ù)類(lèi)型錯(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é)果為失敗。
上面我們只是斷言了異常的類(lèi)型。但有的時(shí)候我們想更進(jìn)一步斷言異常的說(shuō)明信息,pytest也可以做到。with pytest.raises()執(zhí)行結(jié)束后會(huì)生成一個(gè)ExceptionInfo的實(shí)例對(duì)象。該對(duì)象包含type , value, traceback屬性。value屬性就是我們需要的異常說(shuō)明信息。
見(jiàn)示例:
# ./func.py
def add(a,b):
if isinstance(a,int) and isinstance(b,int):
return a+b
else:
raise TypeError('數(shù)據(jù)類(lèi)型錯(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)
# 加入該不通過(guò)斷言為了查看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ù)類(lèi)型錯(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”就是異常的信息,包含類(lèi)型,異常說(shuō)明,異常跟蹤信息。
可以通過(guò)assert斷言這些信息。
也可以通過(guò)給pytest.raises()傳入match關(guān)鍵字參數(shù)來(lái)完成E.value的斷言,這里運(yùn)用到的是python中正則表達(dá)式的原理。
示例:
該示例意味斷言通過(guò)
def test_add_by_func_aaa():
with pytest.raises(TypeError, match=r'.*類(lèi)型錯(cuò)誤$') as E:
add('3',4)
該示例意味斷言失?。?/p>
# 異常測(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ù)類(lèi)型錯(cuò)誤'
test_case\test_func.py:14: AssertionError
'''
如果,某個(gè)測(cè)試用例可能出現(xiàn)不同的預(yù)期異常,只要爆出的異常在預(yù)期的幾個(gè)異常之內(nèi),那么如何斷言呢。解決方法很簡(jiǎn)單,原理和接口都沒(méi)變,只是在pytest.raises()中傳入異常類(lèi)型的參數(shù),從傳入一個(gè)異常類(lèi)型,改變?yōu)閭魅胍粋€(gè)異常類(lèi)型組成的元組。同樣只是傳入一個(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ù)類(lèi)型錯(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)行方式,雖然只是一種錦上添花的改變。我們通過(guò)編寫(xiě)hook函數(shù)來(lái)改變pytest的行為。hook函數(shù)是pytest提供的,有很多,各個(gè)hook函數(shù)的詳細(xì)定義應(yīng)該參考pytest的官方文檔。
為失敗斷言自定義說(shuō)明信息是通過(guò)pytest_assertrepr_compare這個(gè)hook函數(shù)完成的。
先看沒(méi)有編寫(xiě)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
'''
再看編寫(xiě)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)行效果。所以編寫(xiě)第三方插件一般是使用這些hook函數(shù)。
到此這篇關(guān)于pytest之a(chǎn)ssert斷言的具體使用的文章就介紹到這了,更多相關(guān)pytest assert斷言內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家徹底理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù),文章通過(guò)如何創(chuàng)建MySQL數(shù)據(jù)表?如何向MySQL表中插入數(shù)據(jù)?如何查詢MySQL中的數(shù)據(jù)?的三個(gè)問(wèn)題展開(kāi)了詳細(xì)的內(nèi)容介紹2022-06-06
Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式
今天小編就為大家分享一篇Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
python正則表達(dá)式re.search()的基本使用教程
正則表達(dá)式是我們?nèi)粘i_(kāi)發(fā)中必然會(huì)遇到的,下面這篇文章主要介紹了python正則表達(dá)式re.search()的基本使用教程,需要的朋友可以參考下2021-05-05
ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實(shí)例
這篇文章主要介紹了ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實(shí)例,具有很好的參考價(jià)值,希望對(duì)有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python實(shí)現(xiàn)清除文件夾中重復(fù)視頻
本文將利用Python中的os、hashlib、shutil模塊實(shí)現(xiàn)對(duì)文件夾中的重復(fù)視頻進(jìn)行清除,實(shí)現(xiàn)文件夾中無(wú)重復(fù)文件情況發(fā)生,需要的可以參考一下2022-05-05
wxpython多線程防假死與線程間傳遞消息實(shí)例詳解
今天小編就為大家分享一篇wxpython多線程防假死與線程間傳遞消息實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12

