【pytest-13】skip,skipif 跳过用例

前言

  1. pytest.mark.skip 可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能
  2. 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例
  3. 实际常见场景:跳过非windows平台上的仅windows测试,或者跳过依赖于当前不可用的外部资源(例如数据库)的测试

一、skip

1、其中跳过执行,其他用例互不影响

 

  • @pytest.mark.skip
  • 跳过执行执行用例,有可选参数reason,跳过的原因,会在执行结果中打印

 

示例:方法,类里的用例,整个类

#encoding:utf-8
#@Time:2020/12/7 21:36
#@Author:sunny

import pytest

def test_a():
    print("我是测试用例a")

@pytest.mark.skip("类外的方法,也可以跳过不执行")
def test_b():
    print("我是测试用例b")

class Test1:

    @pytest.fixture()
    def login(self):
        print("我是登录")

    @pytest.fixture()
    def register(self):
        print("我是注册")

    def test_login(self):
        print("我是测试登录")

    @pytest.mark.skip(reason="类里面的注册函数跳过,注册功能未完善")
    def test_register(self):
        print("我是测试注册")

@pytest.mark.skip(reason="整个类也可以跳过")
class TestSkip:
    def test_1(self):
        print("我是测试用例1")
    def test_2(self):
        print("我是测试用例2")

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

运行结果:

Testing started at 21:59 ...============================= test session starts =============================collected 6 items

test_skip.py .我是测试用例a
s
Skipped: 类外的方法,也可以跳过不执行
.我是测试登录
s
Skipped: 类里面的注册函数跳过,注册功能未完善
s
Skipped: 整个类也可以跳过
s
Skipped: 整个类也可以跳过
                                                      [100%]

======================== 2 passed, 4 skipped in 0.08s =========================
Process finished with exit code 0

注意:

  1. @pytest.mark.skip可以加在类外方法、类中函数和类上
  2. 可以传递一个可选的原因@pytest.mark.skip(reason="xxx")
  3. 如果加在里上面,类里面的所有测试用例都会跳过
  4. 各自跳过,不影响其他用例执行

2、强制跳过,不继续往下执行

pytest.skip("我这条及后面的都跳过不执行")

类似于python的循环,break就跳出循环

def test_function():
    n = 1
    while True:
        print(f"这是我第{n}条用例")
        n += 1
        if n == 5:
            pytest.skip("我跑五次了不跑了")


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

运行结果:

Testing started at 22:23 ...============================= test session starts =============================collected 1 item

test_skip.py s这是我第{n}条用例
这是我第{n}条用例
这是我第{n}条用例
这是我第{n}条用例

Skipped: 我跑五次了不跑了
                                                           [100%]

============================= 1 skipped in 0.05s ==============================
Process finished with exit code 0

3、模块级别跳过整个模块

pytest.skip(msg="",allow_module_level=False)

import sys
import pytest

if sys.platform.startswith("win"):
    pytest.skip("skipping windows-only tests", allow_module_level=True)


@pytest.fixture()
def test_0():
    print("我是测试用例0")

def test_1():
    print("我是测试用例1")

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

运行结果:

Testing started at 22:29 ...============================= test session starts =============================
Skipped: skipping windows-only tests
collected 0 items / 1 skipped

============================= 1 skipped in 0.02s ==============================
Process finished with exit code 0

 二、skipif

1、有条件的跳过

@pytest.mark.skipif(condition, reason="")

  1. 有条件的跳过某些内容,则可以用skipif
  2. 这个是适用于<py3.6版本的解释器上运行的
  3. condition需要返回True才会跳过
import sys

@pytest.mark.skipif(sys.platform == 'win32', reason="does not run on windows")
class TestSkipIf(object):
    def test_function(self):
        print("不能在window上运行")

执行结果:

Testing started at 11:19 ...============================= test session starts =============================collected 1 item

test_skip.py s
Skipped: does not run on windows
                                                           [100%]

============================= 1 skipped in 0.01s ==============================
Process finished with exit code 0

2、跳过标记

  1. 可以将oytest.mark.skip和pytest.mark.skipif赋值给一个标记变量
  2. 在不同模块之间共享这个标记变量
  3. 若有多个模块的测试用例需要用到相同的skip或skipif,可以用一个单独的文件去管理这些通用标记,然后适用于整个测试用例集

示例:

import sys

skipmark=pytest.mark.skip(reason="我用在类上和方法上,不能在window上运行=====")
skipifmark=pytest.mark.skipif(sys.platform=='win32',reason="我用在类里面,不能在window上运行哈哈哈哈")

@skipmark
class TestSkip_mark(object):
    @skipifmark
    def test_01(self):
        print("测试标记")
    def test_02(self):
        print("测试标记")

@skipmark
def test_skip():
    print("测试标记")


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

运行结果:

Testing started at 15:05 ...============================= test session starts =============================collected 3 items

test_skip.py s
Skipped: 我用在类里面,不能在window上运行哈哈哈哈
s
Skipped: 我用在类上和方法上,不能在window上运行=====
s
Skipped: 我用在类上和方法上,不能在window上运行=====
                                                         [100%]

============================= 3 skipped in 0.03s ==============================
Process finished with exit code 0

3、如果缺少某些导入,则跳过模块中的所有测试

pytest.importorskip( modname: str, minversion: Optional[str] = None, reason: Optional[str] = None )

参数列表:

  • modname:模块名
  • minversion:版本号
  • reason:跳过原因,默认不给也行
expect=pytest.importorskip("expect",minversion="0.3")

@expect
def test_import():
    print("test")

运行结果:

Skipped: could not import 'expect': No module named 'expect'
collected 0 items / 1 skipped

 

posted @ 2020-12-08 16:12  做一只热爱生活的小透明  阅读(120)  评论(0)    收藏  举报