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. Portswigger web security academy:OS command injection

    Portswigger web security academy:OS command injection 目录 Portswigger web security academy:OS command ...

  2. (邹博ML)矩阵和线性代数

    主要内容 矩阵 特征值和特征向量 矩阵求导 矩阵 SVD的提法 奇异值分解(Singular Value Decomposition)是一种重要的矩阵分解方法,可以看做对称方阵在任意矩阵上的推广. 假 ...

  3. 【springMVC】<mvc:annotation-driven />标签的使用、作用?

    不牵扯源码的显式的作用 在使用interceptor时,显式的作用. 这是不配置<mvc:annotation-driven/>标签时的public boolean preHandle(H ...

  4. 深度解析对象的hashcode和equals的差异,以及String的内存分配方式

    Q:Java对象的hashcode是怎么得到的 A:Java对象的hashcode是native方法,不是通过Java实现的.hashcode的值是根据对象的内存地址得到的一串数字. Q:如果两个对象 ...

  5. multiset容器erase函数的误用

    <从缺陷中学习C/C++>第3章库函数问题,本章主要介绍库函数的使用中会遇到的问题.使用库函数可以降低软件开发的难度,提高代码编写的效率.本节为大家介绍multiset容器erase函数的 ...

  6. C++ primer plus读书笔记——第5章 循环和关系表达式

    第5章 循环和关系表达式 1. cout.setf(ios_base::boolalpha); cout << (100 > 3) << endl;将输出true,而不是 ...

  7. python 键盘中断子线程及graceful exiting方案

    最近需要实现一个服务程序的graceful exiting,保证在退出前关闭所有已创建的子线程 python借助KeyboardInterrupted异常响应键盘中断,因此首先尝试在子线程中try-c ...

  8. [bug] IDEA 右侧模块灰色

    参考 https://blog.csdn.net/weixin_44188501/article/details/104717177

  9. Rust模块化

    Rust模块化 模块化有助于代码的管理和层次逻辑的清晰 Rust模块化有多种方式: 1.嵌套模块 嵌套模块就是直接在要使用模块的文件中声明模块 mod food{//声明模块 pub struct C ...

  10. Mysql_二进制方式安装详解

    mysql 安装 1.安装方式 1.二进制安装 2.源码包安装 3.rpm包安装 1.二进制安装 1)上传或者下载包 [root@db01 ~]# rz #或者 [root@web01 ~]# wge ...