16-pytest-skip跳过用例

Posted 爱学习de测试小白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了16-pytest-skip跳过用例相关的知识,希望对你有一定的参考价值。

目录

前言

skip跳过测试方法

skip跳过测试类型

skif满足条件跳过

类和方法混合使用skipif

skip多处调用

方法内跳过


前言

  • 在自动化测试中,会有满足某个条件才执行某部分用例,否则跳过用例,这时可以使用skip来实现这个功能。

skip跳过测试方法

  • pytest.mark.skip(reason='原因')
  • 执行时加-r选项,展示跳过原因
# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海
# @File    : test_30.py

import pytest


# 直接跳过方法,reason 标记跳过原因,可省略   执行时加r选项,展示跳过原因
@pytest.mark.skip(reason='这是标记跳过用例的原因')
def test_login():
    print('这是登录用例')


def test_logout():
    print('这是登出用例')


class TestHomepage(object):

    def test_one(self):
        print('这是case1')

        assert 1 == 1

    @pytest.mark.skip()
    def test_two(self):
        print('这是case2')

        assert 2 == 2


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

skip跳过测试类型

# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海


import pytest


# 直接跳过测试类,reason 标记跳过原因,可省
@pytest.mark.skip()
class TestHomepage(object):

    def test_one(self):
        print('这是case1')

        assert 1 == 1

    def test_two(self):
        print('这是case2')

        assert 2 == 2


class TestMyPage(object):

    def test_three(self):
        print('这是case3')

        assert 1 == 1

    def test_four(self):
        print('这是case4')

        assert 2 == 2


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

skif满足条件跳过

# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海
# @File    : test_32.py

import pytest

app_version = 7.0

"""
1. @pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例')
2. 被标记的类或方法,条件为布尔值,当条件为ture时,会被跳过,否则不跳过
3. reason 标记跳过原因,可省
"""


@pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
class TestHomepage(object):

    def test_one(self):
        print('这是case1')

        assert 1 == 1

    def test_two(self):
        print('这是case2')

        assert 2 == 2


@pytest.mark.skipif(app_version > 8.0, reason='APP版本大于等于8.0时不执行')
class TestMyPage(object):

    def test_three(self):
        print('这是case3')

        assert 1 == 1

    def test_four(self):
        print('这是case4')

        assert 2 == 2


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

类和方法混合使用skipif

  • 多个skipif时,满足1个条件即跳过

# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海
# @File    : test_33.py

import pytest

app_version = 9.0

@pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
class TestHomepage(object):

    def test_one(self):
        print('这是case1')

        assert 1 == 1

    @pytest.mark.skipif(app_version <= 9.0, reason='此用例APP版本小于等于9.0时不执行')
    def test_two(self):
        print('这是case2')

        assert 2 == 2


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

skip多处调用

  • 变量名 = pytest.mark.skipif(条件, reason=原因)
# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海
# @File    : test_34.py

import pytest

app_version = 9.0

app_version_nine = pytest.mark.skipif(app_version <= 9.0, reason='此用例APP版本小于等于9.0时不执行')


@pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
class TestHomepage(object):

    def test_one(self):
        print('这是case1')

        assert 1 == 1

    @app_version_nine
    def test_two(self):
        print('这是case2')

        assert 2 == 2


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

方法内跳过

  • pytest.skip(reason='原因')
# -*- coding: utf-8 -*-
# @Time    : 2021/10/24
# @Author  : 大海
# @File    : test_35.py

import pytest

app_version = 9.0


@pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
class TestHomepage(object):

    def test_one(self):
        print('这是case1')
        pytest.skip('跳过此case')
        assert 1 == 1

    def test_two(self):
        print('这是case2')

        assert 2 == 2


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

以上是关于16-pytest-skip跳过用例的主要内容,如果未能解决你的问题,请参考以下文章

Python Unittest - 根据不同测试环境跳过用例详解

python接口自动化-unittest跳过用例

python接口自动化-unittest跳过用例

python接口自动化-unittest跳过用例

跳过用例skip

HttpRunner 跳过用例录制生成用例用例分层机制