pytest框架笔记(七) : skip跳过用例

pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试。常见示例是在非windows平台上跳过仅限windows的测试或跳过依赖于当前不可用的外部资源(例如数据库)。

xfail意味着您希望测试由于某种原因而失败。一个常见的例子是对功能的测试尚未实施。或尚未修复的错误。当测试通过时尽管预计会失败(标有pytest.mark.xfail),它是一个xpass,将在测试摘要中报告。

pytest计数并分别列出skip和xfail测试。未显示有关跳过 / xfailed测试的详细信息默认情况下,以避免混乱输出。您可以使用-r选项查看与"short"字母对应的详细信息显示在测试进度中

pytest -rxXs     #show extra info on xfailed,xpassed,and skipped tests

一、skip

跳过测试函数的最简单方法就是使用跳过装饰器标记它,可以传递一个可选的原因

import pytest

@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
    assert 1==1

运行结果

Testing started at 11:15 PM ...
/usr/local/bin/python3.6 /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_pytest_runner.py --target test_class007.py::test_the_unknown
Launching py.test with arguments test_class007.py::test_the_unknown in /Users/luozelin/Desktop/pytest/pytest_demo

============================= test session starts ==============================
platform darwin -- Python 3.6.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /Users/luozelin/Desktop/pytest/pytest_demo
plugins: html-1.21.1, rerunfailures-7.0, metadata-1.8.0collected 1 item

test_class007.py s
Skipped: no way of currently testing this
                                                       [100%]

============================== 1 skipped in 0.02s ==============================
Process finished with exit code 0

或者,也可以通过调用来在测试执行或设置期间强制跳过pytest.skip(reason)功能:

#!encoding=utf-8
import pytest

def add(a,b):
    return a+b

def test_function():
    c=add(1,2)
    if c!=4:
        pytest.skip("c等于{},但是不等于4,跳过测试")

if __name__=="__main__":
    pytest.main('-s test_class007.py')

运行结果

<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值