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

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐