前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest标记预期失败得测试用例@pytest.mark.xfail()

Pytest标记预期失败得测试用例@pytest.mark.xfail()

作者头像
王大力测试进阶之路
发布2020-07-23 11:39:28
8470
发布2020-07-23 11:39:28
举报

除了测试函数中使用这个方法pytest.xfail()外,xfail还有一种使用方法。就是@pytest.mark.xfail()标记预期会失败的用例,即期望测试用例是失败的,但是不会影响测试用例的的执行。

「标记的用例运行后,断言失败,所以结果是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,也没有像正常一样显示出错误用例及具体信息」

#!/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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 橙子探索测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档