pytest skip函数
来源:互联网 发布:普罗米修斯白板的软件 编辑:程序博客网 时间:2024/05/18 01:49
在实际工作中,经常需要skip过某个test case,比如现阶段某个feature还没有开发完毕,但是先把test cases写到了pytest中,因此需要先把这个test case给skip掉。所以skip在pytest中有很大的用途,掌握这个skip功能会极大地提升工作效率。
这里就列一下我所知道的skip掉test case的方法。
方法一:
使用@pytest.mark.skip(reason='')这个装饰器。以下是pytest官网的一个例子:
@pytest.mark.skip(reason="no way of currently testing this")def test_the_unknown(): ...但是这个功能需要pytest 2.9才能支持,之前的版本亲测是不可用的。
另外一个是skipif装饰器,很多时候,我并不想某个test case都被skip,我只想在特定的一些情况下skip,其他情况照常执行,skipif就是解决这个问题的。以下也是pytest官网的一个例子:
import sys@pytest.mark.skipif(sys.version_info < (3,3), reason="requires python3.3")def test_function(): ...上面例子的含义是该test case的执行需要依赖于Python版本,如果Python版本低于3.3则会被skip,如果版本高于或等于3.3则会被执行。
方法二:
使用pytest.skip(reason=''),注意这个就不再是一个装饰器了,而仅仅是一个函数了。此外需要注意的是,pytest.skip()放置位置对test case的执行至关重要。如果只放在一个test case中,则仅仅会skip掉这一个test case,比如:
import pytestdef test_123(): pytest.skip("Not implemented") assert 1 == 0def test_234(): assert 1 == 0
执行结果如下:
======================================== test session starts =========================================platform linux2 -- Python 2.7.5 -- py-1.4.22 -- pytest-2.6.0plugins: orderingcollected 2 itemstest_123.py sF============================================== FAILURES ==============================================______________________________________________ test_234 ______________________________________________ def test_234():> assert 1 == 0E assert 1 == 0test_123.py:8: AssertionError================================ 1 failed, 1 skipped in 0.01 seconds =================================因为pytest.skip()放在了test_123()中,因此,仅仅会skip掉test_123,test_234()还是会执行的。如果把pytest_skip()放在了test_函数之外,则整个文件下的test cases都会被skip掉而不被执行。如下所示:
import pytestpytest.skip("Not implemented")def test_123(): assert 1 == 0def test_234(): assert 1 == 0执行结果如下:
py.test test_123.py -v======================================== test session starts =========================================platform linux2 -- Python 2.7.5 -- py-1.4.22 -- pytest-2.6.0 -- /usr/bin/pythonplugins: orderingcollected 0 items / 1 skipped===================================== 1 skipped in 0.01 seconds ======================================
0 0
- pytest skip函数
- 【Pytest】fixture函数介绍(重点)
- 【Pytest】pytest断言
- 使用pytest
- pytest fixture
- pytest使用说明
- 【Pytest】pytest的基本设置
- 【Pytest笔记】pytest-pythonpath插件的使用
- 【Pytest】python单元测试框架pytest简介
- poj 2756(java的skip函数保证输入数字)
- pytest相关问题解析
- pytest的plugin
- pytest+jenkins安装配置
- python 测试框架 pytest
- pytest学习笔记
- pytest paramiko 连接超时
- pytest的fixture
- Python pytest测试
- leetcode-19-Remove Nth Node From End of List
- iOS 统计代码行数
- Android安全攻防战,反编译与混淆技术完全解析(下)
- 网络仿真工具TOTEM之——mpls构建LSP
- 计算机视觉/机器学习/深度学习预备知识
- pytest skip函数
- UICollectionView
- DPM(Defomable Parts Model) 源码分析-检测(二)
- 图片缓存思路
- Ajax
- U-Boot串口下载
- React Native开源图片缩放处理组件
- linux 操作命令(1)
- 算法之选择排序