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后执行")
相关推荐
牛马工作号2 小时前
Mosquitto + ACME:从 Docker Compose 部署到 TLS 证书自动续期与加载
运维·docker·容器
强哥聊算力4 小时前
《算力江湖·英伟达传奇》第七集:ChatGPT出现前夜,英伟达发生了什么?
服务器·人工智能·算法·ai·chatgpt·gpu算力
Initialize-le5 小时前
Nginx 504 超时、499 一堆?网关排查法
运维·nginx
砚凝霜7 小时前
【软考信息安全】第二十二章 Web服务器安全分析与增强——Apache与IIS
服务器·前端·安全
Android系统攻城狮7 小时前
Linux Gstreamer深度解析之gst_audio_ring_buffer_delay调用流程与实战(七十二)
linux·运维·服务器·gstreamer音视频·音视频进阶
快乐的韭菜根根8 小时前
ubuntu 安装slurm并配置
linux·运维·ubuntu
数字新视界8 小时前
机房动环监控提升运维效率
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
U-Mail邮件系统8 小时前
自建邮箱服务器有哪些软件?三款主流邮件服务器软件推荐
运维·服务器
倒头就睡的小比特9 小时前
Linux基础指令3
linux·服务器
Smoothcloud润云9 小时前
PyTorch推理服务模型热切换与失败回滚实战:降低版本更新中断
服务器·人工智能·pytorch·python·深度学习·gpu