pytest.mark.skip()功能一点通

为了避免由于环境发生改变或者其他原因造成的测试脚本的阻塞,可用skip跳过某些用例。
1.@pytest.mark.skip()的功能是跳过,可装饰于类与方法。括号内(reason=‘跳过原因’)
2.@pytest.mark.skipif()的功能是跳过,可装饰于类与方法。括号内(判断, reason=‘跳过原因’)
3.pytest.skip()也是跳过,不同于1的是它一般用于方法内,当进行if条件判断后执行该语句。1则更类似于一个烙印,打上后直接跳过。

import pytest


class Test1(object):
    def test_1(self):
        print('1')


class Test2(object):
    @pytest.mark.skip(reason='跳过Test2的test_2')
    def test_2(self):
        print('2')

    def test_3(self):
        print('3')

@pytest.mark.skip(reason='跳过Test3')
class Test3(object):
    def test_4(self):
        print('4')

    def test_5(self):
        print('5')

def reason():
    r = 1
    return r


class Test4(object):
    @pytest.mark.skipif(reason()==1, reason='跳过test_6')
    def test_6(self):
        print('6')

    def test_7(self):
        print('7')

    def test_8(self):
        if True:
            pytest.skip('跳过test_8')
        else:
            print('8')
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1 -- E:\pythonPJ\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\pythonPJ\ruhr_cloud, configfile: pytest.ini
plugins: allure-pytest-2.8.40
collecting ... collected 8 items

展示板.py::Test1::test_1 
展示板.py::Test2::test_2 
展示板.py::Test2::test_3 
展示板.py::Test3::test_4 
展示板.py::Test3::test_5 
展示板.py::Test4::test_6 PASSED                                          [ 12%]1
SKIPPED (跳过Test2的test_2)                     [ 25%]
Skipped: 跳过Test2的test_2
PASSED                                          [ 37%]3
SKIPPED (跳过Test3)                             [ 50%]
Skipped: 跳过Test3
SKIPPED (跳过Test3)                             [ 62%]
Skipped: 跳过Test3
SKIPPED (跳过test_6)                            [ 75%]
Skipped: 跳过test_6

展示板.py::Test4::test_7 PASSED                                          [ 87%]7

展示板.py::Test4::test_8 SKIPPED (跳过test_8)                            [100%]
Skipped: 跳过test_8


======================== 3 passed, 5 skipped in 0.04s =========================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值