写点什么

软件测试|教你用 skip 灵活跳过用例

  • 2023-10-16
    北京
  • 本文字数:3764 字

    阅读完需:约 12 分钟

前言

日常工作中,我们难免会遇到本次执行不需要所有用例都跑一遍的情况,或者说,我们就是希望某些用例不执行,来看看报错。


那除了我们手动去注释掉部分用例,还有没有其他的办法自动地跳过部分用例呢?


Pytest 很懂我们,真的很懂我们,给我们提供了 skip 方法,可以帮助我们实现需求。

skip 用法介绍

  • pytest.main(['-vs','test01.py']) 用-vs 执行,跳过原因才会显示 SKIPPED [1] test01.py:415: 跳过 Test 类,会跳过类中所有方法

  • skip 跳过,无条件和原因 @pytest.mark.skipif()

  • skip 跳过,无需满足条件 true、有跳过原因 @pytest.mark.skipif(reason='无条件,只有跳过原因')

  • skip 跳过,需满足条件 true、且有跳过原因 @pytest.mark.skipif(条件 1==1,reason='跳过原因')

  • skip 赋值变量,多处调用 myskip=pytest.mark.skipif(1==1,reason='skip 赋值给变量,可多处调用')然后 @myskip 使用

跳过测试类

@pytest.mark.skip()和 @pytest.mark.skipif()两个标签,用他们装饰测试类


  1. @pytest.mark.skip()被标记的类中所有方法测试用例都会被跳过


import pytest

@pytest.mark.skip(reason='跳过TestSkip类中的所有方法')class TestSkip(object): def test01(self): print('test01') assert 1 == 1
def test02(self): print('test02') assert 1 == 1

if __name__ == '__main__': pytest.main(['-vs', 'test_skip.py'])
-----------------------test_skip.py::TestSkip::test01 SKIPPED (跳过TestSkip类中的所有方法)Skipped: 跳过TestSkip类中的所有方法
test_skip.py::TestSkip::test02 SKIPPED (跳过TestSkip类中的所有方法)Skipped: 跳过TestSkip类中的所有方法

============================= 2 skipped in 0.04s ==============================
复制代码


  1. 被标记的类,当条件为 ture 时,会被跳过,否则不跳过被标记的类,当条件成立时,会跳过类中的所有方法


import pytest
@pytest.mark.skipif(1 == 1, reason='当条件成立,跳过类中的所有方法')class TestSkipif(object): def test03(self): print('test03') assert 3 == 3
def test04(self): print('test04') assert 4 == 4
if __name__ == '__main__': pytest.main(['-vs', 'test_skip.py'])
--------------------------test_skip.py::TestSkipif::test03 SKIPPED (当条件成立,跳过类中的所有方法)Skipped: 当条件成立,跳过类中的所有方法
test_skip.py::TestSkipif::test04 SKIPPED (当条件成立,跳过类中的所有方法)Skipped: 当条件成立,跳过类中的所有方法
============================= 2 skipped in 0.04s ==============================
复制代码


被标记的类,当条件不成立时,不会跳过类中的所有方法


import pytest
@pytest.mark.skipif(1 == 3, reason='当条件不成立,不跳过类中的所有方法')class TestSkipif(object): def test03(self): print('test03') assert 3 == 3
def test04(self): print('test04') assert 4 == 4
if __name__ == '__main__': pytest.main(['-vs', 'test_skip.py'])-----------------------
test_skip.py::TestSkipif::test03 test03PASSEDtest_skip.py::TestSkipif::test04 test04PASSED
============================== 2 passed in 0.04s ==============================
复制代码

跳过方法或测试用例

我们想要某个方法或跳过某条用例,在方法上加以下 3 种都可以


  • 跳过方法或用例,未备注原因 @pytest.mark.skip()

  • 跳过方法或用例,备注原因 @pytest.mark.skip(reason='跳过一个方法或一个测试用例')

  • 当条件满足,跳过方法或用例,备注原因 @pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例')


import pytest
class TestClass(object): # 跳过方法,未备注原因 @pytest.mark.skip() def test_one(self): print('test_one')
# 跳过方法,并备注原因 @pytest.mark.skip(reason='跳过有原因') def test_two(self): print('test_two')
# 当条件满足时,跳过方法,并备注原因 @pytest.mark.skipif(1 == 1, reason='条件成立,跳过有原因') def test_three(self): print('test_three')

if __name__ == '__main__': pytest.main(['-vs', 'test_skip.py'])----------------------------------------
test_skip.py::TestClass::test_one SKIPPED (unconditional skip)Skipped: unconditional skip
test_skip.py::TestClass::test_two SKIPPED (跳过有原因)Skipped: 跳过有原因
test_skip.py::TestClass::test_three SKIPPED (条件成立,跳过有原因)Skipped: 条件成立,跳过有原因

============================= 3 skipped in 0.04s ==============================
复制代码

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

  1. 如果类中的条件满足,无论方法中的条件是否满足,均跳过该类下的所有方法,如下所示:


import pytest@pytest.mark.skipif(1 == 1, reason='当类中条件满足,会跳过类中的所有方法')class TestClass(object):    @pytest.mark.skip(reason='跳过不执行')    def test1(self):        print('test1')
# 类中条件不满足,方法中条件满足,跳过不执行 @pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行') def test2(self): print('test2')
# 类中条件不满足,方法中条件也不满足,不跳过继续执行 @pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行') def test3(self): print('test3')
if __name__ == '__main__': pytest.main(['-vs', 'test_demo.py'])
------------------------------------------============================= test session starts =============================collecting ... collected 3 items
test_demo.py::TestClass::test1 SKIPPED (当类中条件满足,会跳过类中的所有方法) [ 33%]Skipped: 当类中条件满足,会跳过类中的所有方法
test_demo.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) [ 66%]Skipped: 条件满足,跳过不执行
test_demo.py::TestClass::test3 SKIPPED (当类中条件满足,会跳过类中的所有方法) [100%]Skipped: 当类中条件满足,会跳过类中的所有方法

============================= 3 skipped in 0.02s ==============================
复制代码


  1. 如果类中不满足条件,方法中满足条件,跳过方法;如果类中不满足条件,方法中也不满足条件,继续执行方法中的代码;如下所示:


import pytest
@pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过') # 类中不满足条件class TestClass(object): @pytest.mark.skip(reason='跳过不执行') def test1(self): print('test1')
# 类中条件不满足,方法中条件满足,跳过不执行 @pytest.mark.skipif(1 == 1, reason='条件满足,跳过不执行') def test2(self): print('test2')
# 类中条件不满足,方法中条件也不满足,不跳过继续执行 @pytest.mark.skipif(1 == 2, reason='条件不满足,不跳过,继续执行') def test3(self): print('test3')

if __name__ == '__main__': pytest.main(['-vs', 'test_demo.py'])
------------------------------------
============================= test session starts =============================collecting ... collected 3 items
test_demo.py::TestClass::test1 SKIPPED (跳过不执行) [ 33%]Skipped: 跳过不执行
test_demo.py::TestClass::test2 SKIPPED (条件满足,跳过不执行) [ 66%]Skipped: 条件满足,跳过不执行
test_demo.py::TestClass::test3 PASSED [100%]test3

======================== 1 passed, 2 skipped in 0.02s =========================
复制代码

pytest.skip()方法内跳过

除了通过使用标签的方式,还可以在测试用例中调用 pytest.skip()方法来实现跳过,可以选择传入 reason 参数来说明跳过原因;如果想要通过判断是否跳过,可以写在 if 判断里(_)


import pytest
class TestClass(object):
def test001(self): if 'h' in 'hell': pytest.skip(reason='跳过,不执行') # 不执行后面的代码 print('test001')
def test002(self): print('test002')

if __name__ == '__main__': pytest.main(['-vs', 'test_demo.py'])
============================= test session starts =============================collecting ... collected 2 items
test_demo.py::TestClass::test001 SKIPPED (跳过,不执行) [ 50%]Skipped: 跳过,不执行
test_demo.py::TestClass::test002 PASSED [100%]test002

======================== 1 passed, 1 skipped in 0.02s =========================
复制代码

总结

关于 pytest 跳过用例执行的介绍就到这里,我们可以灵活使用 skip 的特性,提升我们的工作效率!


获取更多技术资料,请点击!

用户头像

社区:ceshiren.com 微信:ceshiren2021 2019-10-23 加入

微信公众号:霍格沃兹测试开发 提供性能测试、自动化测试、测试开发等资料,实时更新一线互联网大厂测试岗位内推需求,共享测试行业动态及资讯,更可零距离接触众多业内大佬。

评论

发布
暂无评论
软件测试|教你用skip灵活跳过用例_霍格沃兹测试开发学社_InfoQ写作社区