pytest skip的使用
skip跳过用例(无条件跳过,不运行用例)
使用方法:
1.使用跳过装饰器
class TestClass():
@pytest.mark.skip(reason='no way of currently testing this') #标记为skip后,该用例不会执行
def test_one(self):
print("test_one方法执行")
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love' def test_three(self):
print("test_three方法执行")
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 3 items
test_gy.py::TestClass::test_one SKIPPED [ 33%]
test_gy.py::TestClass::test_two PASSED [ 66%]
test_gy.py::TestClass::test_three PASSED [100%]
======================================================================================================= 2 passed, 1 skipped in 0.28s =======================================================================================================
2.pytest.skip(reason):在测试执行或设置期间强制跳过
import pytest class TestClass():
def test_one(self):
print("test_one方法执行")
if 1==1:
pytest.skip("skip") #如果if语句为True时,执行到这里会跳过,方法后面的代码都不会执行
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love' def test_three(self):
print("test_three方法执行")
assert 3-2==1
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 3 items
test_gy.py::TestClass::test_one SKIPPED [ 33%]
test_gy.py::TestClass::test_two PASSED [ 66%]
test_gy.py::TestClass::test_three PASSED [100%]
======================================================================================================= 2 passed, 1 skipped in 0.22s =======================================================================================================
3.pytest.skip(reason,allow_module_level=True):跳过整个模块
import pytest
if 1 == 1:
pytest.skip("skip", allow_module_level=True) class TestClass():
def test_one(self):
print("test_one方法执行")
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love' def test_three(self):
print("test_three方法执行")
assert 3-2==1
运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 0 items / 1 skipped
============================================================================================================ 1 skipped in 0.31s ===================
skip If跳过用例(有条件跳过)
1.装饰器:
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
import pytest
class TestClass():
name='one'
@pytest.mark.skipif(name=='one',reason="skip") #如果装饰器里面的条件满足会跳过改方法
def test_one(self):
print("test_one方法执行")
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love' 运行结果:
C:\Users\cale\checkapi\test>pytest test_gy.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items test_gy.py::TestClass::test_one SKIPPED [ 50%]
test_gy.py::TestClass::test_two PASSED [100%] ======================================================================================================= 1 passed, 1 skipped in 0.17s =======================================================================================================
2.模块之间共享skipif标记
test_m1.py:在模块中定义一个装饰器name_one
import pytest name = 'one'
name_one = pytest.mark.skipif(name == "one", reason='skip') #定义装饰器name_one class TestClass():
@name_one #调用装饰器
def test_one(self):
print("test_one方法执行")
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love'
test_m2.py:导入test_m1.py模块,并调用它的name_one装饰器
import pytest
from test_cc.test_m1 import name_one @name_one #导入test_gy模块并调用它的装饰器name_one
def test_three():
print("test_three方法执行")
assert 1==1 def test_four():
print("test_four方法执行")
assert 'l' in 'liao'
运行test_m2.py模块的测试用例:
C:\Users\cale\checkapi\test_cc>pytest test_m2.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test_cc
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items test_m2.py::test_three SKIPPED [ 50%]
test_m2.py::test_four PASSED [100%] ======================================================================================================= 1 passed, 1 skipped in 0.14s =======================================================================================================
3.skipif装饰类:当条件成立时,类中的所有方法都不会执行
import pytest,sys @pytest.mark.skipif(sys.platform=='win32',reason='skip')
class TestClass():
def test_one(self):
print("test_one方法执行")
assert 1==1 def test_two(self):
print("test_two方法执行")
assert 'o' in 'love'
运行结果:
C:\Users\cale\checkapi\test_cc>pytest test_m1.py -v
=========================================================================================================== test session starts ============================================================================================================
platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\ipharmacare\python37\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.7.3', 'Platform': 'Windows-10-10.0.17134-SP0', 'Packages': {'pytest': '5.2.1', 'py': '1.8.0', 'pluggy': '0.13.0'}, 'Plugins': {'html': '2.0.0', 'metadata': '1.8.0', 'rerunfailures': '7.0'}}
rootdir: C:\Users\cale\checkapi\test_cc
plugins: html-2.0.0, metadata-1.8.0, rerunfailures-7.0
collected 2 items
test_m1.py::TestClass::test_one SKIPPED [ 50%]
test_m1.py::TestClass::test_two SKIPPED [100%]
============================================================================================================ 2 skipped in 0.13s ============================================================================================================
pytest skip的使用的更多相关文章
- 10、pytest -- skip和xfail标记
目录 1. 跳过测试用例的执行 1.1. @pytest.mark.skip装饰器 1.2. pytest.skip方法 1.3. @pytest.mark.skipif装饰器 1.4. pytest ...
- pytest 10 skip跳过测试用例
pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者你希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试.常见事例时非win ...
- pytest八:skip 跳过用例
这是一个快速指南,介绍如何在不同情况下跳过模块中的测试1.无条件地跳过模块中的所有测试:pytestmark = pytest.mark.skip("all tests still WIP& ...
- Pytest权威教程12-跳过(Skip)及预期失败(xFail): 处理不能成功的测试用例
目录 跳过(Skip)及预期失败(xFail): 处理不能成功的测试用例 Skip跳过用例 xFail:将测试函数标记为预期失败 Skip/xFail参数设置 返回: Pytest权威教程 跳过(Sk ...
- Pytest系列(7) - skip、skipif跳过用例
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest.mark.sk ...
- pytest测试框架 -- skip跳过执行测试用例
跳过执行测试用例 1.@pytest.mark.skip(reason=" ") -- 跳过执行测试函数 可传入一个非必须参数reason表示原因 import pytest@ ...
- Pytest学习(七) - skip、skipif的使用
前言 作为一个java党,我还是觉得pytest和testng很像,有时候真的会感觉到代码语言在某种程度上是相通的,那么今天来说说这两个知识点. skip和skipif,见名知意,就是跳过测试呗,直白 ...
- Pytest(9)skip跳过用例
前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...
- pytest学习笔记(三)
接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...
随机推荐
- thinkphp中常用到的sql操作
1.清空某表数据: $sql = 'truncate table table_name'; Db::execute($sql );
- 【】maven 配置启动tomcat版本,修改默认的6.x.x版本
<build> <plugins> <!-- 配置Tomcat插件 ,用于启动项目 --> <plugin> <groupId>org.ap ...
- 【SpringMVC】添加操作时返回400
本博客老魏原创,如需转载请留言 问题描述: springmvc向数据库添加新的记录时,发生400错误,控制台没有抛出异常. 问题原因: 视图中的提交数据的某一个字段不不匹配导致. 解决方法: 不要怀疑 ...
- 『动善时』JMeter基础 — 12、JMeter取样器详解:sampler
目录 1.取样器介绍 2.JMeter自带的取样器 3."HTTP请求"为例介绍一下取样器 (1)HTTP Request: (2)Web服务器: (3)HTTP请求: (4)同请 ...
- ElasticSearch第三弹之存储原理
我们上文中介绍的ES内部索引的写处理流程是在ES的内存中执行的,而数据被分配到特定的主.副分片上之后,最终是存储到磁盘上的,这样在断电的时候就不会丢失数据.具体的存储路径可在配置文件 ../confi ...
- Nios II系统在Quartus II编译后Timing requirements for slow timing model timing analysis were not met. See Report window for details
来自http://wenku.baidu.com/link?url=h0Z_KvXD3vRAn9H8mjfbVErVOF_Kd3h-BZSyF1r4sEYj3ydJGEfBHGY1mvntP4HDuF ...
- Mac 搭建 Sentry
Sentry 为我们提供应用程序的错误跟踪,使我们能够快速定位到错误所在的文件和行号. 以下是官网支持语言和框架的部分截图: 准备工作 自 2020 年 12 月 4 日起,Sentry 默认使用 P ...
- 5Spring动态代理开发小结
5Spring动态代理开发小结 1.为什么要有动态代理? 好处 1.利于程序维护 2.利于原始类功能的增强 3.得益于JDK或者CGlib等动态代理技术使得程序扩展性很强 为什么说使得程序扩展性很强? ...
- Docker 中运行 ElasticSearch 和 Kibana
ElasticSearch 是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口:Kibana 是一个开源的分析和可视化平台,可以搜索,查看. ...
- [bug] Python Anoconda3 安装完成后开始菜单不显示
版本问题,需更新 win+R打开cmd,敲入命令: conda update menuinst conda install -f console_shortcut ipython ipython-no ...