pytest测试框架系列 - Pytest skip和skipif 跳过用例看这篇就够了!

前言

  • skip和skipif,看名字就是跳过测试的意思,主要用于不想执行的代码,标记后,标记的代码不执行。
  • 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  • 实际常见场景:根据平台不同执行测试、跳过依赖、功能未完成预期不能执行的测试

有哪些用例跳过方式

  • 跳过单个测试用例
  • 跳过测试类
  • 跳过测试模块
  • 通过条件跳过执行case
  • 自定义跳过标识

跳过单个测试用例

  • @pytest.mark.skip(reason=" ") 跳过执行测试用例

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:15
# @Author  : king
# @File    :test_skip.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

@pytest.mark.skip(reason="跳过测试函数的测试case")
def test_01():
    print("我是测试函数。")
    assert 1 == 1

class TestSkip:
    @pytest.mark.skip(reason="跳过类里面case")
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

  • pytest.skip(msg="") 在测试函数内部使用

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:15
# @Author  : king
# @File    :test_skip.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def test_01():
    print("我是测试函数。")
    pytest.skip(msg="跳过此条用例。")
    assert 1 == 1

class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        pytest.skip(msg="跳过此条用例。")
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

跳过测试类

  • @pytest.mark.skip(reason=" ")

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:15
# @Author  : king
# @File    :test_skip.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest

def test_01():
    print("我是测试函数。")
    assert 1 == 1

@pytest.mark.skip(reason="跳过下面测试类")
class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

跳过测试模块

  • pytestmark = pytest.mark.skip(reason="") pytestmark 不可更改变量名,让他等于标签即可

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:15
# @Author  : king
# @File    :test_skip.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
pytestmark = pytest.mark.skip(reason="跳过当前模块")

def test_01():
    print("我是测试函数。")
    assert 1 == 1

class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

  • pytest.skip("", allow_module_level=True)

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:15
# @Author  : king
# @File    :test_skip.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
import sys

if not sys.platform.startswith("win"):
    pytest.skip("下面用例仅在win下执行", allow_module_level=True)

def test_01():
    print("我是测试函数。")
    assert 1 == 1

class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

通过条件跳过执行case

  • @pytest.mark.skipif(condition=“表达式”, reason="") – 若满足condition,则跳过测试函数

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:37
# @Author  : king
# @File    :test_skip_if.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
import sys

@pytest.mark.skipif(sys.version_info < (3, 8), reason="python版本不低于3.8")
def test_01():
    print("我是测试函数。")
    assert 1 == 1

@pytest.mark.skipif(condition=sys.version_info < (3, 8), reason="python版本不低于3.8")
class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"

if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

自定义跳过标识

  • 使用myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=…)装饰时用该变量代替标签即可:@myskip

原因:如果多个case需要使用同一个原因时,可以少些很多代码,增强代码可读性

示例:

# !/usr/bin/python3
# _*_coding:utf-8 _*_

""""
# @Time  :2021/7/5 20:37
# @Author  : king
# @File    :test_skip_if.py
# @Software  :PyCharm
# @blog     :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""
import pytest
import sys

mark_version = pytest.mark.skipif(sys.version_info < (3, 8), reason="python版本不低于3.8")
# mark_version = pytest.mark.skip(reason="部分功能未完成")

@mark_version
def test_01():
    print("我是测试函数。")
    assert 1 == 1

class TestSkip:
    def test_one(self, ):
        print("test_one方法执行")
        assert 1 == 1

    @mark_version
    def test_two(self):
        print("test_two方法执行")
        assert "king" in "hello,king"


if __name__ == '__main__':
    pytest.main()

执行结果:
在这里插入图片描述

总结

  • @pytest.mark.skip(reason="") 跳过测试用例执行,放在单个测试用例上跳过单个,放在类上面,跳过类里面的所有的用例,reason为选填参数
  • @pytest.mark.skipif(condition=表达式, reason="") 跳过测试用例执行,放在单个测试用例上跳过单个,放在类上面,跳过类里面的所有的用例,reason为必填参数
  • pytestmark = pytest.mark.skip(reason="") 或 pytest.skip(reason="", allow_module_level=True),跳过当时py文件里面的所有用例,格式不能改变

以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!

如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章
个人微信号

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用\[1\]:在pytest中,可以使用`pytest.skip`函数来跳过测试用例。可以在测试函数或测试类中使用`pytest.skip`函数来指定跳过的原因。例如,在测试函数中使用`pytest.skip`函数可以跳过测试函数,并在跳过时提供一个消息。示例代码如下: ```python import pytest def test_01(): print("我是测试函数。") pytest.skip(msg="跳过此条用例。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): pytest.skip(msg="跳过此条用例。") print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,`test_01`函数和`test_two`方法都使用了`pytest.skip`函数来跳过测试用例,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 引用\[2\]:另一种在pytest跳过测试用例的方法是使用`pytest.mark.skip`装饰器。可以在测试函数或测试类上使用`pytest.mark.skip`装饰器来指定跳过的原因。示例代码如下: ```python import pytest pytestmark = pytest.mark.skip(reason="跳过当前模块") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了整个模块跳过状态,并提供了跳过的原因。当运行这个模块测试用例时,所有的测试用例都将被跳过执行。 引用\[3\]:还可以使用`pytest.mark.skip`装饰器来跳过测试函数或测试类。示例代码如下: ```python import pytest @pytest.mark.skip(reason="跳过测试函数的测试case") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: @pytest.mark.skip(reason="跳过类里面case") def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了`test_01`函数和`test_one`方法为跳过状态,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 综上所述,pytest中可以使用`pytest.skip`函数或`pytest.mark.skip`装饰器来跳过测试用例,并提供跳过的原因。 #### 引用[.reference_title] - *1* *2* *3* [pytest测试框架系列 - Pytest skipskipif 跳过用例这篇了!](https://blog.csdn.net/u010454117/article/details/118469127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

测试之路king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值