pytest除了測試函數中使用這個方法pytest.xfail()外,xfail還有一種使用方法。就是@pytest.mark.xfail()標記預期會失敗的用例,即期望測試用例是失敗的,但是不會影響測試用例的的執行。
標記的用例運行後,斷言失敗,所以結果是xfailed,也沒有像正常一樣顯示出錯誤用例及具體信息。
預期會失敗,實際斷言失敗xfailed
#!/usr/bin/env python
# _*_coding:utf-8_*_
import pytest
class 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 item
test_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 pytest
class 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 item
test_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