Pytest之pytest.assume用例中断言1失败会继续执行后续代码断言2
生活随笔
收集整理的這篇文章主要介紹了
Pytest之pytest.assume用例中断言1失败会继续执行后续代码断言2
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ?一般我們做自動化測試時,一個用例會寫多個斷言,當第一個斷言失敗后,后面的代碼就不會執行了,于是我們引進了pytest-assume插件可以解決斷言失敗后繼續斷言的問題。
一、安裝依賴包
pip install pytest-assume
二、使用assert進行斷言
我們發現test_01用例中,第一個斷言assert 0 == 1失敗時,后面的代碼不會再執行,?print('執行test_01斷言2')和assert 1 == 2未執行。
#!/usr/bin/env python # _*_coding:utf-8_*_ import pytestclass Test(object):def test_01(self):"""用例1"""print('執行test_01斷言1')assert 0 == 1print('執行test_01斷言2')assert 1 == 2def test_02(self):"""用例2"""print('執行test_02斷言1')assert 3 == 3print('執行test_02斷言2')assert 4 == 4if __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 itemstest_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 == 1test_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進行斷言,用法pytest.assume(預期結果?== 實際結果)
我們發現test_01用例中,第一個斷言pytest.assume(0 == 1)失敗時,print('執行test_01斷言2')和pytest.assume(1 == 2)仍執行了,說明同一個用例中使用pytest.assume進行斷言,如果第一個斷言失敗了,后面的代碼仍會繼續執行。
#!/usr/bin/env python # _*_coding:utf-8_*_ import pytestclass 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 itemstest_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 = Nonedef reraise(tp, value, tb=None):try:if value is None:value = tp()if value.__traceback__ is not tb: > raise value.with_traceback(tb) E pytest_assume.plugin.FailedAssumption: E 2 Failed Assumptions: E E test_C_01.py:12: AssumptionFailure E >> pytest.assume(0 == 1) E AssertionError: assert False E E test_C_01.py:15: AssumptionFailure E >> pytest.assume(1 == 2) E 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?
?
?
總結
以上是生活随笔為你收集整理的Pytest之pytest.assume用例中断言1失败会继续执行后续代码断言2的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python测试开发django5.te
- 下一篇: Python一行代码给儿子制作九九乘法表