Python Pytest自動化測試 斷言失敗後續程式碼繼續執行pytest.assume
阿新 • • 發佈:2020-11-28
Time will tell.
做自動化測試時我們一般會一個用例寫多個斷言,而當第一個斷言失敗後,後面的程式碼就不會執行。這時我們可以引進pytest-assume
外掛來解決這些問題。
一、安裝
pip install pytest-assume
二、案例
在test_01
用例中,第一個斷言assert 0 == 1
失敗,後面的程式碼不會執行。
import pytest
class Test(object):
def test_01(self):
"""用例1"""
print('執行test_01斷言1')
assert 0 == 1
print('執行test_01斷言2')
assert 1 == 2
def test_02(self):
"""用例2"""
print('執行test_02斷言1')
assert 3 == 3
print('執行test_02斷言2')
assert 4 == 4
if __name__ == '__main__':
pytest.main(['-s', 'test_C_01.py'])
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/程式碼/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\程式碼\Test\test
plugins: assume-2.2.1
收集的測試用例:[<Function test_01>, <Function test_02>]
排序後收集的測試用例:[<Function test_01>, <Function test_02>]
collected 2 items
test_C_01.py 執行test_01斷言1
F執行test_02斷言1
執行test_02斷言2
.
================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________
self = <test.test_C_01.Test object at 0x0000018C74AAEF48>
def test_01(self):
"""用例1"""
print('執行test_01斷言1')
> assert 0 == 1
E assert 0 == 1
test_C_01.py:11: AssertionError
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - assert 0 == 1
========================= 1 failed, 1 passed in 0.06s =========================
Process finished with exit code 0
三、使用pytest.assume
預期結果 == 實際結果。
在test_01
用例中,第一個斷言pytest.assume(0 == 1)
失敗後,print('執行test_01斷言2')
和pytest.assume(1 == 2)
仍執行了。
說明同一用例中使用pytest.assume
進行斷言,即便第一個失敗了,後面的程式碼也會繼續執行。.
import pytest
class Test(object):
def test_01(self):
"""用例1"""
print('執行test_01斷言1')
pytest.assume(0 == 1)
print('執行test_01斷言2')
pytest.assume(1 == 2)
def test_02(self):
"""用例2"""
print('執行test_02斷言1')
pytest.assume(3 == 3)
print('執行test_02斷言2')
pytest.assume(4 == 4)
if __name__ == '__main__':
pytest.main(['-s', 'test_C_01.py'])
C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/程式碼/Test/test/test_C_01.py
============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\Users\admin\Desktop\程式碼\Test\test
plugins: assume-2.2.1
收集的測試用例:[<Function test_01>, <Function test_02>]
排序後收集的測試用例:[<Function test_01>, <Function test_02>]
collected 2 items
test_C_01.py 執行test_01斷言1
執行test_01斷言2
F執行test_02斷言1
執行test_02斷言2
.
================================== FAILURES ===================================
________________________________ Test.test_01 _________________________________
tp = <class 'pytest_assume.plugin.FailedAssumption'>, value = None, tb = None
def reraise(tp, value, tb=None):
try:
if value is None:
value = tp()
if value.__traceback__ is not tb:
raise value.with_traceback(tb)
pytest_assume.plugin.FailedAssumption:
2 Failed Assumptions:
test_C_01.py:12: AssumptionFailure
>> pytest.assume(0 == 1)
AssertionError: assert False
test_C_01.py:15: AssumptionFailure
>> pytest.assume(1 == 2)
AssertionError: assert False
..\..\..\..\AppData\Roaming\Python\Python37\site-packages\six.py:702: FailedAssumption
=========================== short test summary info ===========================
FAILED test_C_01.py::Test::test_01 - pytest_assume.plugin.FailedAssumption:
========================= 1 failed, 1 passed in 0.20s =========================
Process finished with exit code 0
好嘍,以上就是本章內容嘍,如果你對Python自動化軟體測試等更多內容感興趣可以加入我們扣裙一起學習175317069。有各項測試學習資源發放,更有行業深潛多年的技術人分析講解。
祝你能成為一名優秀的軟體測試工程師!
覺得還不錯就【點贊】、【評論】、【關注】吧~
Time will tell.(時間會證明一切)
http://www.autono1.com/news/manufacturer/2020-11-27/49661.html