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 ...
随机推荐
- Windows核心编程 第2 4章 异常处理程序和软件异常
异常处理程序和软件异常 C P U引发的异常,就是所谓的硬件异常(hardware exception).操作系统和应用程序 也可以引发相应的异常,称为软件异常(software exception) ...
- windows-API劫持(API-HOOK)
API Hook ApiHook又叫做API劫持,也就是如果A程序调用了B.cll里面的C函数,我们可以做到当A调用C函数执行的时候,直接执行我们自己事先准备好的函数,之后我们在执行真正的C,当然我们 ...
- 【python】Leetcode每日一题-设计停车系统
[python]Leetcode每日一题-设计停车系统 [题目描述] 请你给一个停车场设计一个停车系统.停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位. 请你实现 Parki ...
- Day003 注释、标识符和关键字
注释.标志符.关键字 注释 当项目逐渐复杂,注释就很重要了. 注释不会被执行,是给我们写代码的人看的. 书写注释是一个非常好的习惯. Java中的注释有三种: 当行注释 // 多行注释 /* 注释 * ...
- PhpStorm 配置本地文件自动上传至服务器
目的:本地文件夹下的文件实时同步至指定服务器的文件夹,减少代码移植的成本和风险 添加一个SFTP连接 Tools - Deployment - Browse Remote Host 配置连接参数 Co ...
- 提升50%!Presto如何提升Hudi表查询性能?
分享一篇关于使用Hudi Clustering来优化Presto查询性能的talk talk主要分为如下几个部分 演讲者背景介绍 Apache Hudi介绍 数据湖演进和用例说明 Hudi Clust ...
- Zabbix 5.0 优化建议
Blog:博客园 个人 在使用Zabbix过程中,正确的调整Zabbix系统,使之保持高性能是非常重要的,能够充分利用硬件资源,监控更多主机和性能指标. 硬件 关于zabbix server端硬件的建 ...
- Linux下线程pid和tid
#include <stdio.h> #include <pthread.h> #include <sys/types.h> #include <sys/sy ...
- FHD 4K 8K分辨率
4K(2160P,即4096×2160的像素分辨率)和8K(4320P,即7,680 × 4,320的像素分辨率)属于UHDTV. FHD是FULL HD(Full High Definition)的 ...
- UVA 160 - Factors and Factorials
Factors and Factorials The factorial of a number N (written N!) is defined as the product of all t ...