Pytest标记预期失败得测试用例@pytest.mark.xfail()
生活随笔
收集整理的這篇文章主要介紹了
Pytest标记预期失败得测试用例@pytest.mark.xfail()
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? pytest除了測試函數中使用這個方法pytest.xfail()外,xfail還有一種使用方法。就是@pytest.mark.xfail()標記預期會失敗的用例,即期望測試用例是失敗的,但是不會影響測試用例的的執行。
標記的用例運行后,斷言失敗,所以結果是xfailed,也沒有像正常一樣顯示出錯誤用例及具體信息。
預期會失敗,實際斷言失敗xfailed
#!/usr/bin/env python # _*_coding:utf-8_*_ import pytestclass Test(object):@pytest.mark.xfail(reason="預期失敗")def test_login_01(self):"""用例1"""print('執行用例test_login_01斷言1')pytest.assume(1 == 0)print('執行用例test_login_01斷言2')pytest.assume(2 == 2)if __name__ == '__main__':pytest.main(['-v', '-s', 'test_01.py'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py ============================= test session starts ============================= platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe cachedir: .pytest_cache rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01 plugins: assume-2.2.1, ordering-0.6 collecting ... 收集的測試用例:[<Function test_login_01>] collected 1 itemtest_01.py::Test::test_login_01 執行用例test_login_01斷言1 執行用例test_login_01斷言2 XFAIL============================= 1 xfailed in 0.07s ==============================Process finished with exit code 0標記的用例運行后,斷言成功,所以結果是xfailed,也沒有像正常一樣顯示出錯誤用例及具體信息
預期會失敗,實際斷言成功xpassed
#!/usr/bin/env python # _*_coding:utf-8_*_ import pytestclass Test(object):@pytest.mark.xfail()def test_login_02(self):"""用例2"""print('執行用例test_login_02斷言1')pytest.assume(3 == 3)print('執行用例test_login_02斷言2')pytest.assume(True)if __name__ == '__main__':pytest.main(['-v', '-s', 'test_01.py'])C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe C:/Users/admin/Desktop/AutoTest/Test/test/test_01/test_01.py ============================= test session starts ============================= platform win32 -- Python 3.7.4, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Users\admin\AppData\Local\Programs\Python\Python37\python.exe cachedir: .pytest_cache rootdir: C:\Users\admin\Desktop\AutoTest\Test\test\test_01 plugins: assume-2.2.1, ordering-0.6 collecting ... 收集的測試用例:[<Function test_login_02>] collected 1 itemtest_01.py::Test::test_login_02 執行用例test_login_02斷言1 執行用例test_login_02斷言2 XPASS============================= 1 xpassed in 0.04s ==============================Process finished with exit code 0?
總結
以上是生活随笔為你收集整理的Pytest标记预期失败得测试用例@pytest.mark.xfail()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JMeter之HTTP请求上传文件/上传
- 下一篇: echarts自定义showlading