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的使用的更多相关文章

  1. 10、pytest -- skip和xfail标记

    目录 1. 跳过测试用例的执行 1.1. @pytest.mark.skip装饰器 1.2. pytest.skip方法 1.3. @pytest.mark.skipif装饰器 1.4. pytest ...

  2. pytest 10 skip跳过测试用例

    pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者你希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试.常见事例时非win ...

  3. pytest八:skip 跳过用例

    这是一个快速指南,介绍如何在不同情况下跳过模块中的测试1.无条件地跳过模块中的所有测试:pytestmark = pytest.mark.skip("all tests still WIP& ...

  4. Pytest权威教程12-跳过(Skip)及预期失败(xFail): 处理不能成功的测试用例

    目录 跳过(Skip)及预期失败(xFail): 处理不能成功的测试用例 Skip跳过用例 xFail:将测试函数标记为预期失败 Skip/xFail参数设置 返回: Pytest权威教程 跳过(Sk ...

  5. Pytest系列(7) - skip、skipif跳过用例

    如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest.mark.sk ...

  6. pytest测试框架 -- skip跳过执行测试用例

      跳过执行测试用例 1.@pytest.mark.skip(reason=" ") -- 跳过执行测试函数 可传入一个非必须参数reason表示原因 import pytest@ ...

  7. Pytest学习(七) - skip、skipif的使用

    前言 作为一个java党,我还是觉得pytest和testng很像,有时候真的会感觉到代码语言在某种程度上是相通的,那么今天来说说这两个知识点. skip和skipif,见名知意,就是跳过测试呗,直白 ...

  8. Pytest(9)skip跳过用例

    前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 Skip和xfail: 处理那些不会成功的测试用例 你可以对那些在某些特定平台上不能运行的测试用 ...

  9. pytest学习笔记(三)

    接着上一篇的内容,这里主要讲下参数化,pytest很好的支持了测试函数中变量的参数化 一.pytest的参数化 1.通过命令行来实现参数化 文档中给了一个简单的例子, test_compute.py ...

随机推荐

  1. MinGW 可以编译驱动的

    #include <ddk/ntddk.h> static VOID STDCALLmy_unload( IN PDRIVER_OBJECT DriverObject ) {} NTSTA ...

  2. HR:“最喜欢阿里出来的程序员了,技术又好又耐艹!” 我:???

    面试造火箭,进厂拧螺丝?真的是这样吗? 缘起 估计不少同学都是被标题吸引进来的.事先声明,这句话不是我虚构的,而是我实实在在从同事的口中听到的,而且还不止一次. 当时的场景就是很正常的交谈,别人也并没 ...

  3. PTA 第三章 栈与队列

    一.判断题 1.若一个栈的输入序列为1,2,3,--,N,输出序列的第一个元素为i,则第j个输出的元素是j-i-1    (×)解析:应该是不确定的,不能保证数字出栈后不会再入栈 2.所谓" ...

  4. mysql枚举和集合

    create table consumer( id int, name char(16), sex enum('male','female','other'), level enum('vip1',' ...

  5. 5分钟让你理解K8S必备架构概念,以及网络模型(中)

    写在前面 在这用XMind画了一张导图记录Redis的学习笔记和一些面试解析(源文件对部分节点有详细备注和参考资料,欢迎关注我的公众号:阿风的架构笔记 后台发送[导图]拿下载链接, 已经完善更新): ...

  6. golang:net/http理解总结

    Go语言标准库内建提供了net/http包,涵盖了HTTP客户端和服务端的具体实现.使用net/http包,我们可以很方便地编写HTTP客户端或服务端的程序. http服务端的创建流程 在使用http ...

  7. [DB] Hadoop免密登录原理及设置

    情景: 现有两台电脑bigdata111.bigdata112,bigdata111想免密码登录bigdata112 过程: 1.bigdata111生成公钥(用于加密,给别人)和私钥(用于解密,自己 ...

  8. WPS-插入-公式-菜单 怎样在EXCEL中使用PRODUCT函数

    怎样在EXCEL中使用PRODUCT函数 ################   WPS2018 -插入-公式-[专门有公式菜单] 插入函数       ##################       ...

  9. 与find不同,locate并不是实时查找。你需要更新数据库,以获得最新的文件索引信息。updatedb

    find是实时查找,如果需要更快的查询,可试试locate:locate会为文件系统建立索引数据库,如果有文件更新,需要定期执行更新命令来更新索引库: $locate string 寻找包含有stri ...

  10. Samba服务配置及配置文件说明

    前言 1.配置Samba服务为什么要关闭防火墙(firewalld)和Selinux? 在linux操作系统中默认开启了防火墙,Selinux也处于启动状态,一般状态为enforing:所以,在我们搭 ...