Pytest skip&&skipif跳过⽤例

Pytest skip&&skipif跳过⽤例

跳过用例:@pytest.mark.skip @pytest.mark.skipif

skip和skipif可以标记希望跳过的测试用例。要给跳过的测试添加理由和条件,应当使用skipif。

区别:

skip和skipf区别:skip无条件跳过;skipif有条件的跳过

复制代码
import pytest

@pytest.mark.skip(reason="test11暂不执行")
def test_11():
	print("正在执行测试用例-------test_11")
	a = ['a','b','c',1,2,3]
	b = 'a'
	assert b in a

@pytest.mark.skipif(2>1,reason="因为2>1成立所以暂不执行")
def test_12():
	print("正在执行测试用例-------test_12")
	x = 'a'
	y = 'b'
	assert x != y

def test_13():
	print("正在执行测试用例-------test_13")
	assert 3>2
	print("assert后执行")

def test_14():
	print("正在执行测试用例-------test_14")
	assert 1>2
	print("assert后执行")

def test_15():
	print("正在执行测试用例-------test_15")
	pytest.assume(2>=2)
	pytest.assume('a'=='a')
	pytest.assume(2 in [1,2,3,4])
	pytest.assume( 1 != 2)
	print("assert后执行")

执行结果:

复制代码
test_mod5.py::test_11 SKIPPED (test11暂不执行)
test_mod5.py::test_12 SKIPPED (因为2>1成立所以暂不执行)
test_mod5.py::test_13 正在执行测试用例-------test_13
assert后执行
PASSED
test_mod5.py::test_14 正在执行测试用例-------test_14
FAILED
test_mod5.py::test_15 正在执行测试用例-------test_15
assert后执行
PASSED

======================================================================================== FAILURES ========================================================================================
________________________________________________________________________________________ test_14 _________________________________________________________________________________________

    def test_14():
        print("正在执行测试用例-------test_14")
>       assert 1>2
E    assert 1 > 2

test_mod5.py:24: AssertionError
================================================================================ short test summary info =================================================================================
FAILED test_mod5.py::test_14 - assert 1 > 2
========================================================================= 1 failed, 2 passed, 2 skipped in 0.39s =========================================================================

可以将 pytest.mark.skip 和 pytest.mark.skipif 赋值给一个标记变量单独封装成一个模块在不同模块之间导入以达到集中管理这些通用标记的目的,然后适用于整个测试用例集

复制代码
# mark_skip.py
import sys
import pytest

skip_by_versions = pytest.mark.skip(reason="xxx版本暂不执行")
skip_by_platform = pytest.mark.skipif(sys.platform=='win32',reason=f"{sys.platform}平台暂不执行")

# test_mod5.py
import pytest
from mark_skip import *

@skip_by_versions
def test_11():
	print("正在执行测试用例-------test_11")
	a = ['a','b','c',1,2,3]
	b = 'a'
	assert b in a

@skip_by_platform
def test_12():
	print("正在执行测试用例-------test_12")
	x = 'a'
	y = 'b'
	assert x != y

def test_13():
	print("正在执行测试用例-------test_13")
	assert 3>2
	print("assert后执行")

@skip_by_versions
def test_14():
	print("正在执行测试用例-------test_14")
	assert 1>2
	print("assert后执行")

def test_15():
	print("正在执行测试用例-------test_15")
	pytest.assume(2>=2)
	pytest.assume('a'=='a')
	pytest.assume(2 in [1,2,3,4])
	pytest.assume( 1 != 2)
	print("assert后执行")
相关推荐
buhuizhiyuci1 小时前
【Linux 篇】数字世界的水流蓄水池 —— IO 缓冲区机制深度实战解析
linux·运维·服务器·c++
贝锐1 小时前
多校区互通、临时联网、远程运维,校园网络升级用蒲公英更省心!
运维·组网
骇客野人1 小时前
Linux 服务器 Python 版 MCP 服务部署整体方案(适配 Claude/Cursor/自研Agent)
linux·服务器·python
啦啦啦啦啦zzzz2 小时前
moduo网络库 Acceptor TcpConnection
服务器·网络·c++·reactor·网络库
游戏开发爱好者82 小时前
App Store 上传 IPA 自动化,.p8 密钥认证与 CI/CD 接入实战
android·运维·ci/cd·小程序·uni-app·自动化·iphone
天疆说2 小时前
04 稳定性与性能调优实录:OOM 崩溃、并发槽位、前缀缓存与 DSpark 调参
linux·运维·缓存
微硬创新2 小时前
大点数模拟量采集落地:耐达讯自动化32路0-20mA适配PROFINET总线实践解析
运维·人工智能·网络协议·自动化·信息与通信
星卯教育tony2 小时前
NOI Linux 2.0 服务器多用户网页桌面部署方案(腾讯云轻量Ubuntu20.04专属版 CSP复赛比赛环境搭建免安装虚拟机 )
linux·服务器·腾讯云
GIS数据转换器3 小时前
村镇无人机物流配送与跨域监测一体化平台
大数据·运维·人工智能·科技·无人机