004、pytest斷言assert
阿新 • • 發佈:2021-07-16
常用斷言
pytest裡面斷言實際上就是python裡面的assert斷言方法,常用的有以下幾種
- assert xx 判斷xx為真
- assert not xx 判斷xx不為真
- assert a in b 判斷b包含a
- assert a == b 判斷a等於b
- assert a != b 判斷a不等於b
assert斷言
a、Python的assert是用來檢查一個條件,如果它為真,就不做任何事。 如果它為假,則會丟擲AssertError並且包含錯誤資訊。
b、在測試用例裡面,斷言的作用就是判斷期望結果和實際結果是否一致 。
c、斷言失敗後,可以加一句錯誤資訊 。
d、如果有多個斷言在同一個函式 / 方法裡的話,所有的斷言都True時,該測試用例才是True 。
示例程式碼如下:
# -*- coding:utf-8 -*- # @Author: Sky # @Email: [email protected] # @Time: 2021/7/16 17:21 def f(): a = 5 return a def test_01(): # 斷言是否為True assert f() def test_02(): # 斷言是否為False assert not f() def test_03(): # 斷言a是否在b裡面 a = 'hello' b = 'hello worldView Code' assert a in b def test_04(): # 斷言a是否在b裡面 a = 'hello' b = 'helloaa' # 斷言失敗後,可以加一句錯誤資訊 assert a == b, 'a 不等於 b' def test_05(): # 斷言a不等於b a = 'hello' b = 'hello world' assert a != b
執行結果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.pyView Code=================================================================================== test session starts ==================================================================================== platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0 collected 5 items test_02.py .F.F. ========================================================================================= FAILURES ========================================================================================= _________________________________________________________________________________________ test_02 __________________________________________________________________________________________ def test_02(): # 斷言是否為False > assert not f() E assert not 5 E + where 5 = f() test_02.py:18: AssertionError _________________________________________________________________________________________ test_04 __________________________________________________________________________________________ def test_04(): # 斷言a是否在b裡面 a = 'hello' b = 'helloaa' # 斷言失敗後,可以加一句錯誤資訊 > assert a == b, 'a 不等於 b' E AssertionError: a 不等於 b E assert 'hello' == 'helloaa' E - helloaa E ? -- E + hello test_02.py:33: AssertionError ================================================================================= short test summary info ================================================================================== FAILED test_02.py::test_02 - assert not 5 FAILED test_02.py::test_04 - AssertionError: a 不等於 b =============================================================================== 2 failed, 3 passed in 0.19s ================================================================================ D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>
如果有多個斷言在同一個函式 / 方法裡的話,所有的斷言都True時,該測試用例才是True 。
# -*- coding:utf-8 -*- # @Author: Sky # @Email: [email protected] # @Time: 2021/7/16 17:21 def f(): a = 5 return a def test_01(): # 如果有多個斷言的話,所有的斷言都True時,該測試用例才是True 。 assert f() < 0 assert f() == 5View Code
執行結果如下:
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>pytest -s .\test_02.py =================================================================================== test session starts ==================================================================================== platform win32 -- Python 3.8.6, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd plugins: allure-pytest-2.9.43, html-2.1.1, metadata-1.11.0 collected 1 item test_02.py F ========================================================================================= FAILURES ========================================================================================= _________________________________________________________________________________________ test_01 __________________________________________________________________________________________ def test_01(): # 如果有多個斷言的話,所有的斷言都True時,該測試用例才是True 。 > assert f() < 0 E assert 5 < 0 E + where 5 = f() test_02.py:13: AssertionError ================================================================================= short test summary info ================================================================================== FAILED test_02.py::test_01 - assert 5 < 0 ==================================================================================== 1 failed in 0.16s ===================================================================================== D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day06\dd>View Code