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. Windows核心编程 第六章 线程基础知识 (下)

    6.6 线程的一些性质 到现在为止,讲述了如何实现线程函数和如何让系统创建线程以便执行该函数.本节将要介绍系统如何使这些操作获得成功. 图6 - 1显示了系统在创建线程和对线程进行初始化时必须做些什么 ...

  2. NumPy中文文档搬砖(划掉)学习笔记(1)

    原文地址 前言 况下加速Python中的操作运行时.适用于快速数值运算的一个选项是NumPy,它当之无愧地将自己称为使用Python进行科学计算的基本软件包. 当然,很少有人将50微秒(百万分之五十秒 ...

  3. 【Git】5. 远程库(GitHub)相关操作

    之前也提到了,在整个协作的过程中,必不可少的就是远程库了.Github作为一个全球最大的同性交友网站,同样也是一个非常强大的远程库. 现在希望将本地的hello.txt文件也推到github上去,那首 ...

  4. 如何在spring boot中从控制器返回一个html页面?

    项目截图 解决方法 我之前用的@RestController注解,而@RestController这个控制器返回数据而不是视图,改成@Controller 就好了(以下是修改后的) @Controll ...

  5. thinkphp 连接多个数据库(tp5.1为例)

    1.config目录下添加数据库配置,内容跟原数据库配置一样就可以(数据库名改成连接的第二个数据库名) 2.连接部分代码: $db = Db::connect(config('database2.') ...

  6. 多线程-5.JMM之happens-before原则

    a happens-before b 翻译为a操作对b操作是可见的.可见即是指共享变量的更改能获知. 特性:传递性 原则:volatile定义的变量 写操作 happens-before 读操作 同一 ...

  7. Django(10)ORM模型介绍

    前言 随着项目越来越大,采用写原生SQL的方式在代码中会出现大量的SQL语句,那么问题就出现了: 1.SQL语句重复利用率不高,越复杂的SQL语句条件越多,代码越长.会出现很多相近的SQL语句. 2. ...

  8. C++逆向分析----多重继承和菱形继承

    多重继承 多重继承是指C++类同时继承两个类或两个以上的类. class Test { public: int num1; Test() { num1 = 1; } virtual void Proc ...

  9. MzzTxx——博客目录

    准备阶段 团队介绍 需求分析 技术规格说明书 功能规格说明书 Alpha 阶段任务分配 团队贡献分分配方案 Scrum Meeting Alpha 2021.04.21 Scrum Meeting 0 ...

  10. 记一次 .NET 某HIS系统后端服务 内存泄漏分析

    一:背景 1. 讲故事 前天那位 his 老哥又来找我了,上次因为CPU爆高的问题我给解决了,看样子对我挺信任的,这次另一个程序又遇到内存泄漏,希望我帮忙诊断下. 其实这位老哥技术还是很不错的,他既然 ...