pytest之a(chǎn)ssert斷言的具體使用
背景
本文總結(jié)使用pytest編寫自動化測試時常用的assert斷言。
說明
本文將從以下幾點做總結(jié):
- 為測試結(jié)果作斷言
- 為斷言不通過的結(jié)果添加說明信息
- 為預(yù)期異常作斷言
- 為失敗斷言自定義說明信息
為測試結(jié)果作斷言
在斷言方面,pytest框架比其他類似的框架(比如unittest)更加簡潔,易用,我想這是我選擇pytest作為自動化測試框架之一的原因之一。
pytest的assert斷言關(guān)鍵字支持使用python內(nèi)置的assert表達式??梢岳斫鉃閜ytest的斷言就是直接使用python自帶的assert關(guān)鍵字。
python assert的概念:
Python assert(斷言)用于判斷一個表達式,在表達式條件為 false 的時候觸發(fā)異常。
我們可以在在assert后面添加任何符合python標(biāo)準的表達式,如果表達式的值通過bool轉(zhuǎn)換后等于False,則意味著斷言結(jié)果為失敗。
以下舉例常用的表達式:
# ./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,所以測試結(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é)果為失敗。
上面我們只是斷言了異常的類型。但有的時候我們想更進一步斷言異常的說明信息,pytest也可以做到。with pytest.raises()執(zhí)行結(jié)束后會生成一個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('數(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的斷言,這里運用到的是python中正則表達式的原理。
示例:
該示例意味斷言通過
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的運行方式,雖然只是一種錦上添花的改變。我們通過編寫hook函數(shù)來改變pytest的行為。hook函數(shù)是pytest提供的,有很多,各個hook函數(shù)的詳細定義應(yīng)該參考pytest的官方文檔。
為失敗斷言自定義說明信息是通過pytest_assertrepr_compare這個hook函數(shù)完成的。
先看沒有編寫pytest_assertrepr_compare這個hook函數(shù)時,默認的失敗斷言說明:
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的運行方式和運行效果。所以編寫第三方插件一般是使用這些hook函數(shù)。
到此這篇關(guān)于pytest之a(chǎn)ssert斷言的具體使用的文章就介紹到這了,更多相關(guān)pytest assert斷言內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)之列表和元組的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家徹底理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09Python練習(xí)之操作MySQL數(shù)據(jù)庫
這篇文章主要介紹了Python練習(xí)之操作MySQL數(shù)據(jù)庫,文章通過如何創(chuàng)建MySQL數(shù)據(jù)表?如何向MySQL表中插入數(shù)據(jù)?如何查詢MySQL中的數(shù)據(jù)?的三個問題展開了詳細的內(nèi)容介紹2022-06-06Python Pandas 轉(zhuǎn)換unix時間戳方式
今天小編就為大家分享一篇Python Pandas 轉(zhuǎn)換unix時間戳方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例
這篇文章主要介紹了ipython jupyter notebook中顯示圖像和數(shù)學(xué)公式實例,具有很好的參考價值,希望對有所幫助。一起跟隨小編過來看看吧2020-04-04