pytest篇8-pytest之skip/skipif跳过用例

01

引言

上一篇总结了pytest.fixture()中scope参数四种不同纬度的运用,今天我们一起总结一下pytest中用例的跳过。其实前面unittest单元测试框架中有总结skip用例的跳过,大家可以一并看看前面的内容,一起灵活运用起来。

# skip的用法
@pytest.mark.skip(reason="不想执行的缘由,执行时会输出reason内容")
# skipif的用法
@pytest.mark.skipif(条件表达式, reason="")
# 当条件表达式返回True时,会跳过用例

02


函数级别跳过

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/4/13 11:06 下午
# @Author : Maynard
# @File : test_07skip.py
# @Software: PyCharm




import pytest
def test_pytest_1():
    print('case1')


@pytest.mark.skip()
def test_pytestt_2():
    print('case2')


@pytest.mark.skip(reason='功能暂时不支持')
def test_pytestt_3():
    print('case3')


@pytest.mark.skipif(1 == 1,reason='满足条件跳过此用例')
def test_pytest_4():
    print('case4')


@pytest.mark.skipif(1 == 2,reason='不满足条件不会跳过此用例')
def test_pytest_5():
    print('case5')


class TestDemo():
    def test_pytest_6(self):
        print('case6')


    @pytest.mark.skip()
    def test_pytest_7(self):
        print('case7')


    @pytest.mark.skip(reason='功能还不支持')
    def test_pytest_8(self):
        print('case8')


    @pytest.mark.skipif(1 == 1,reason='满足条件,跳过')
    def test_pytest_9(self):
        print('case9')


    @pytest.mark.skipif(1 == 2,reason='不满足条件不会跳过')
    def test_pytest_10(self):
        print('case10')
        
if __name__ == '__main__':
    pytest.main(['-s','test_07skip.py'])

运行结果

4条(case1、case5、case6、case10)用例执行通过,其余6条用例被跳过

03


类级别跳过

跳过类中skip和skipif的用法一样,唯一的就是将skip或者skipif的装饰器放在要跳过的类上面,详见如下:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/4/14 10:27 下午
# @Author : Maynard
# @File : test_08skipclass.py
# @Software: PyCharm


import pytest
def test_pytest_1():
    print('case1')


def test_pytest_2():
    print('case2')


class TestDemo1():
    def test_pytest_3(self):
        print('case3')


    def test_pytest_4(self):
        print('case4')


@pytest.mark.skip('跳过类TestDemo2')
class TestDemo2():
    def test_pytest_5(self):
        print('case5')


    def test_pytest_6(self):
        print('case6')


@pytest.mark.skipif(1==1,reason='条件满足,跳过TestDemo3')
class TestDemo3():
    def test_pytest_7(self):
        print('case7')


    def test_pytest_8(self):
        print('case8')


@pytest.mark.skipif(1==2,reason='不条件满足,不会跳过TestDemo4')
class TestDemo4():
    def test_pytest_9(self):
        print('case9')


    def test_pytest_10(self):
        print('case10')


if __name__ == '__main__':
    pytest.main(['-s','test_08skipclass.py'])

运行结果

2 skipped 是代表跳过了两个类(TestDemo2/TestDemo3)里面的测试用例

04


模块级别跳过

目录层级
pytestskipmodule包
-- test_09skipmodule01.py
-- test_10skipmodule02.py
-- test_11skipmodule03.py

test_09skipmodule01.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/4/14 10:38 下午
# @Author : Maynard
# @File : test_09skipmodule01.py
# @Software: PyCharm


import pytest
def test_pytest_1():
    print('case1')


def test_pytest_2():
    print('case2')


class TestDemo1():
    def test_pytest_3(self):
        print('case3')


    def test_pytest_4(self):
        print('case4')


test_10skipmodule02.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/4/14 10:39 下午
# @Author : Maynard
# @File : test_10skipmodule02.py
# @Software: PyCharm


import pytest
pytestmark = pytest.mark.skip(reason ='跳过test_10skipmodule02.py模块')
def test_pytest_5():
    print('case5')


def test_pytest_6():
    print('case6')


class TestDemo3():
    def test_pytest_7(self):
        print('case7')


    def test_pytest_8(self):
        print('case8')

test_11skipmodule03.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2021/4/14 10:39 下午
# @Author : Maynard
# @File : test_11skipmodule03.py
# @Software: PyCharm


import pytest
pytestmark = pytest.mark.skipif(1==1,reason='条件满足,跳过test_11skipmodule03.py模块')
def test_pytest_9():
    print('case9')


def test_pytest_10():
    print('case10')


class TestDemo4():
    def test_pytest_11(self):
        print('case11')


    def test_pytest_12(self):
        print('case12')


运行

# 先cd 进入pytestskipmodule包
pytest -s

运行结果:跳过了两个模块的case

pytest篇8-pytest之skip/skipif跳过用例_第1张图片

05


总结

  1. skipif 中的条件表达式可以使用变量,根据变量返回是True/False来判断是否跳过用例;

  2. 可以灵活运用跳过case、跳过类、跳过模块的纬度来进行实操;

  3. 当某些case不想运行在某些平台上,可以使用skip/skipif;

  4. 开发某些功能未实现,需要跳过某些case;

06


PS

  1. 后续会继续总结pytest框架,个人觉得最强py单元测试框架;

  2. 大家有问题,可以通过公众号首页添加作者微信,多交流、多沟通;

  3. 万丈高楼平地起,利用业余时间多总结,加油!!

  4. 最近本人工作比较忙,所以更新的比较少,后续会挤出时间常更新;

你可能感兴趣的:(单元测试,python,编程语言,nginx,ios)