专栏首页橙子探索测试Pytest跳过执行之@pytest.mark.skip()详解

Pytest跳过执行之@pytest.mark.skip()详解

一、skip介绍及运用

在我们自动化测试过程中,经常会遇到功能阻塞、功能未实现、环境等一系列外部因素问题导致的一些用例执行不了,这时我们就可以用到跳过skip用例,如果我们注释掉或删除掉,后面还要进行恢复操作。

1、skip跳过成功,标识为s ============================= 2 skipped in 0.04s ==============================

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

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

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

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

6、skip赋值变量,多处调用myskip=pytest.mark.skipif(1==1,reason='skip赋值给变量,可多处调用')

然后@myskip使用

二、跳过测试类

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

1、被标记的类中所有方法测试用例都会被跳过

2、被标记的类,当条件为ture时,会被跳过,否则不跳过

#skip跳过类
 
import pytest,sys
@pytest.mark.skip(reason='跳过Test类,会跳过类中所有方法')
class Test(object):
    def test_one(self):
        assert 1==1
    def test_two(self):
        print('test_02')
        assert 1==2
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py ss                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [2] test01.py: 跳过Test类,会跳过类中所有方法
============================= 2 skipped in 0.07s ==============================
 
Process finished with exit code 0
 
 
 
 
#skip满足条件,skip跳过类
import pytest,sys
@pytest.mark.skipif(1==1,reason='跳过Test类,会跳过类中所有方法')
class Test(object):
    def test_one(self):
        assert 1==1
    def test_two(self):
        print('test_02')
        assert 1==2
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py ss                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:415: 跳过Test类,会跳过类中所有方法
SKIPPED [1] test01.py:417: 跳过Test类,会跳过类中所有方法
============================= 2 skipped in 0.04s ==============================
 
Process finished with exit code 0

三、跳过方法或测试用例

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

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

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

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

1、跳过方法或用例,未备注原因
import pytest,sys
class Test(object):
    @pytest.mark.skip()
    def test_one(self):
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py s.                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:414: unconditional skip
======================== 1 passed, 1 skipped in 0.04s =========================
 
Process finished with exit code 0
 
2、跳过方法或用例,备注了原因
import pytest,sys
class Test(object):
    @pytest.mark.skip(reason='跳过一个方法或一个测试用例')
    def test_one(self):
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py s.                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:414: 跳过一个方法或一个测试用例
======================== 1 passed, 1 skipped in 0.05s =========================
 
Process finished with exit code 0
 
3、当条件满足,跳过方法或用例,备注了原因
import pytest,sys
class Test(object):
    @pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例')
    def test_one(self):
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py s.                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:414: 跳过一个方法或一个测试用例
======================== 1 passed, 1 skipped in 0.06s =========================
 
Process finished with exit code 0

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

我们在类和方法上分别加了skip,类中满足条件,方法中未满足条件,所以生效类中skip

import pytest,sys
@pytest.mark.skipif(1==1,reason='多个条件时,有1个条件满足就跳过(类)')
class Test(object):
    @pytest.mark.skipif(1==2, reason='多个条件时,有1个条件满足就跳过(方法)')
    def test_one(self):
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py ss                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:418: 多个条件时,有1个条件满足就跳过(类)
SKIPPED [1] test01.py:415: 多个条件时,有1个条件满足就跳过(类)
============================= 2 skipped in 0.04s ==============================

五、skip赋值给变量,可多出调用

无论是@pytest.mark.skip()标签还是@pytest.mark.skipif()标签,如果你想在多个测试方法上装饰,依次写起来很麻烦的话,你可以选择定义个变量让它等于标签,然后在装饰的时候用该变量代替标签。这种方法,你还可以通过在其他模块中导入的变量的方式,在其他模块中共享标签;如果可以这样的话,我们为什么不新建一个模块用来存放标签呢?这样是不是又方便了许多。

赋值:myskip=pytest.mark.skipif(1==1,reason='skip赋值给变量,可多处调用')

调用:@myskip

import pytest,sys
myskip=pytest.mark.skipif(1==1,reason='skip赋值给变量,可多处调用')
class Test(object):
    @myskip
    def test_one(self):
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py s.                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] test01.py:415: skip赋值给变量,可多处调用
======================== 1 passed, 1 skipped in 0.07s =========================
 
Process finished with exit code 0

六、pytest.skip()方法内跳过

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

import pytest,sys
class Test(object):
    def test_one(self):
        pytest.skip(msg='跳过')
        assert 1==2
    def test_two(self):
        print('test_02')
        assert 1==1
if __name__=='__main__':
    pytest.main(['-rs','test01.py'])
 
 
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: C:\Users\wangli\PycharmProjects\Test\test
plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
collected 2 items
 
test01.py s.                                                             [100%]
 
=========================== short test summary info ===========================
SKIPPED [1] c:\users\wangli\pycharmprojects\test\test\test01.py:416: 跳过
======================== 1 passed, 1 skipped in 0.04s =========================
 
Process finished with exit code 0

本文分享自微信公众号 - 橙子探索测试(chengzitest),作者:王荔探索测试

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-11-30

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Pytest自定义标记mark及指定文件/类/方法/用例执行

    pytest.main(['-s','test01.py','-m=test'])

    橙子探索测试
  • Pytest标记预期失败得测试用例@pytest.mark.xfail()

    除了测试函数中使用这个方法pytest.xfail()外,xfail还有一种使用方法。就是@pytest.mark.xfail()标记预期会失败的用例,即期望测...

    橙子探索测试
  • Pytest Hooks方法之pytest_collection_modifyitems改变测试用例执行顺序

    pytest默认执行用例顺序是根据项目下文件名称按ascii码去收集运行的,文件里的用例是从上往下按顺序执行的.

    橙子探索测试
  • Pytest自定义标记mark及指定文件/类/方法/用例执行

    pytest.main(['-s','test01.py','-m=test'])

    橙子探索测试
  • python pytest测试框架

        2.2使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    于小勇
  • Pytest系列(1) - 快速入门和基础讲解

    https://www.cnblogs.com/poloyy/category/1690628.html

    小菠萝测试笔记
  • [接口测试_B] 02 Pytest的简单示例

    Pytest是什么 Pytest是Python的一个测试工具,可以用于所有类型和级别的软件测试。Pytest是一个可以自动查找到你编写的用例并运行后输出结果的测...

    苦叶子
  • Pytest系列(16)- 分布式测试插件之pytest-xdist的详细使用

    https://www.cnblogs.com/poloyy/category/1690628.html

    小菠萝测试笔记
  • iOS自动化探索(七)自动化测试框架pytest - 测试报告

    JunitXML报告是一种很常用的测试报告,比如可以和Jenkins进行集成,在Jenkins的GUI上显示Pytest的运行结果,非常便利。 运行完case后...

    周希
  • Python测试应用与工具

    Python测试应用与公具 今天跟大家分享一个Python与测试相关的话题,主要介绍Python中的标准库 unittest及第三方测试工具pytest及m...

    1846122963

扫码关注云+社区

领取腾讯云代金券