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后执行")
相关推荐
雾隐隐o4 小时前
Docker Compose 多容器编排实战
运维·docker·容器
姚不倒5 小时前
负载均衡架构设计:F5 ↔ Nginx/HAProxy ↔ Keepalived 全链路映射
运维·网络·nginx
好评1245 小时前
【Linux】应用层自定义协议与序列化
linux·运维·网络
fengyehongWorld5 小时前
Linux caddy的安装与配置
linux·运维·服务器
2401_862880825 小时前
Linux应用层开发 --- HTTP
linux·运维·http
s_w.h5 小时前
【 linux 】线程互斥与同步
java·linux·服务器·开发语言
可爱系程序猿5 小时前
libcrypto-3-x64.dll 加载失败排查:OpenSSL 3 版本、PATH 顺序与 x64 依赖如何对应
linux·运维·服务器·程序人生·电脑
zbyyd5 小时前
Linux 网络编程:IO 多路复用
linux·运维·c语言·网络
fengyehongWorld5 小时前
Linux 域名申请与DNS解析
linux·运维·服务器
weixin_440730505 小时前
pytest结合allure生成html测试报告(step、story、severity、screenshot)
前端·html·pytest·allure报告