跳过执行测试用例,有可选参数reason:跳过的原因,会在执行结果中打印
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = __time__ = 2020/4/9 13:49__author__ = 小菠萝测试笔记__blog__ = https://www.cnblogs.com/poloyy/"""import pytest@pytest.fixture(autou=true)def login(): print("====登录====")def test_ca01(): print("我是测试用例11111")@pytest.mark.skip(reason="不执行该用例!!因为没写好!!")def test_ca02(): print("我是测试用例22222")class test1: def test_1(lf): print("%% 我是类测试用例1111 %%") @pytest.mark.skip(reason="不想执行") def test_2(lf): print("%% 我是类测试用例2222 %%")@pytest.mark.skip(reason="类也可以跳过不执行")class testskip: def test_1(lf): print("%% 不会执行 %%")
执行结果
知识点
@pytest.mark.skip可以加在函数上,类上,类方法上如果加在类上面,类里面的所有测试用例都不会执行以上小案例都是针对:整个测试用例方法跳过执行,如果想在测试用例执行期间跳过不继续往下执行呢?作用:在测试用例执行期间强制跳过不再执行剩余内容
类似:在python的循环里面,满足某些条件则break 跳出循环
def test_function(): n = 1 while true: print(f"这是我第{n}条用例") n += 1 if n == 5: pytest.skip("我跑五次了不跑了")
执行结果
当allow_module_level=true时,可以设置在模块级别跳过整个模块
#!/usr/bin/env python# -*- coding: utf-8 -*-"""__title__ = __time__ = 2020/4/9 13:49__author__回眸一笑的意思 = 小简单生活简单爱菠萝测试笔记__blog__ = https://www.cnblogs.com/poloyy/"""import sysimport pytestif sys.platform.startswith("win"): pytest.skip("skipping windows-only tests", allow_module_level=true)@pytest.fixture(autou=true)def login(): print("====登录==神笔马良课文==")def test_ca01(): print("我是测试用例11111")
执行结果
collecting …
skipped: skipping windows-only tests
collected 0 items / 1 skipped
============================= 1 skipped in 0.15s ==============================
作用:希望有条件地跳过某些测试用例
注意:condition需要返回true才会跳过
@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")class testskipif(object): def test_function(lf): print("不能在window上运行")
执行结果
collecting … collected 1 item
07skip_sipi王恩茂f.py::testskipif::test_function skipped [100%]
skipped: does not run on windows
============================= 1 skipped in 0.04s ==============================
# 标记skipmark = pytest.mark.skip(reason="不能在window上运行=====")skipifmark = pytest.mark.skipif(sys.platform == 'win32', reason="不能在window上运行啦啦啦=====")@skipmarkclass testskip_mark(object): @skipifmark def test_function(lf): print("测试标记") def test_def(lf): print("测试标记")@skipmarkdef test_skip(): print("测试标记")
执行结果
collecting … collected 3 items
07skip_sipif.py::testskip_mark::test_function skipped [ 33%]
skipped: 不能在window上运行啦啦啦=====
07skip_sipif.py::testskip_mark::test_def skipped [ 66%]
skipped: 不能在window上运行=====
07skip_sipif.py::test_skip skipped [100%]
skipped: 不能在window上运行=====
============================= 3 skipped in 0.04s ==============================
作用:如果缺少某些导入,则跳过模块中的所有测试
参数列表
modname:模块名minversion:版本号reasone:跳过原因,默认不给也行pexpect = pytest.importorskip("pexpect", minversion="0.3")@pexpectdef test_import(): print("test")
执行结果一:如果找不到module
skipped: could not import ‘pexpect’: no module named ‘pexpect’
collected 0 items / 1 skipped
执行结果一:如果版本对应不上
skipped: module ‘sys’ has __version__ none, required is: ‘0.3’
collected 0 items / 1 skipped
到此这篇关于pytest中skip skipif跳过用例详解的文章就介绍到这了,更多相关skip skipif跳过用例内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-05 03:13:05,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/0c7fc9cfaa398297a449888167ba9dab.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Pytest中skip skipif跳过用例详解.doc
本文 PDF 下载地址:Pytest中skip skipif跳过用例详解.pdf
留言与评论(共有 0 条评论) |