• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Pytest跳过执行:@pytest.mark.skip()

武飞扬头像
程序员木江
帮助1

一、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时,会被跳过,否则不跳过

  1.  
    #skip跳过类
  2.  
     
  3.  
    import pytest,sys
  4.  
    @pytest.mark.skip(reason='跳过Test类,会跳过类中所有方法')
  5.  
    class Test(object):
  6.  
    def test_one(self):
  7.  
    assert 1==1
  8.  
    def test_two(self):
  9.  
    print('test_02')
  10.  
    assert 1==2
  11.  
    if __name__=='__main__':
  12.  
    pytest.main(['-rs','test01.py'])
  13.  
     
  14.  
     
  15.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  16.  
    ============================= test session starts =============================
  17.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  18.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  19.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  20.  
    collected 2 items
  21.  
     
  22.  
    test01.py ss [100%]
  23.  
     
  24.  
    =========================== short test summary info ===========================
  25.  
    SKIPPED [2] test01.py: 跳过Test类,会跳过类中所有方法
  26.  
    ============================= 2 skipped in 0.07s ==============================
  27.  
     
  28.  
    Process finished with exit code 0
  29.  
     
  30.  
     
  31.  
     
  32.  
     
  33.  
    #skip满足条件,skip跳过类
  34.  
    import pytest,sys
  35.  
    @pytest.mark.skipif(1==1,reason='跳过Test类,会跳过类中所有方法')
  36.  
    class Test(object):
  37.  
    def test_one(self):
  38.  
    assert 1==1
  39.  
    def test_two(self):
  40.  
    print('test_02')
  41.  
    assert 1==2
  42.  
    if __name__=='__main__':
  43.  
    pytest.main(['-rs','test01.py'])
  44.  
     
  45.  
     
  46.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  47.  
    ============================= test session starts =============================
  48.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  49.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  50.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  51.  
    collected 2 items
  52.  
     
  53.  
    test01.py ss [100%]
  54.  
     
  55.  
    =========================== short test summary info ===========================
  56.  
    SKIPPED [1] test01.py:415: 跳过Test类,会跳过类中所有方法
  57.  
    SKIPPED [1] test01.py:417: 跳过Test类,会跳过类中所有方法
  58.  
    ============================= 2 skipped in 0.04s ==============================
  59.  
     
  60.  
    Process finished with exit code 0

三、跳过方法或测试用例

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

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

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

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

  1.  
    1、跳过方法或用例,未备注原因
  2.  
    import pytest,sys
  3.  
    class Test(object):
  4.  
    @pytest.mark.skip()
  5.  
    def test_one(self):
  6.  
    assert 1==2
  7.  
    def test_two(self):
  8.  
    print('test_02')
  9.  
    assert 1==1
  10.  
    if __name__=='__main__':
  11.  
    pytest.main(['-rs','test01.py'])
  12.  
     
  13.  
     
  14.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  15.  
    ============================= test session starts =============================
  16.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  17.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  18.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  19.  
    collected 2 items
  20.  
     
  21.  
    test01.py s. [100%]
  22.  
     
  23.  
    =========================== short test summary info ===========================
  24.  
    SKIPPED [1] test01.py:414: unconditional skip
  25.  
    ======================== 1 passed, 1 skipped in 0.04s =========================
  26.  
     
  27.  
    Process finished with exit code 0
  28.  
     
  29.  
    2、跳过方法或用例,备注了原因
  30.  
    import pytest,sys
  31.  
    class Test(object):
  32.  
    @pytest.mark.skip(reason='跳过一个方法或一个测试用例')
  33.  
    def test_one(self):
  34.  
    assert 1==2
  35.  
    def test_two(self):
  36.  
    print('test_02')
  37.  
    assert 1==1
  38.  
    if __name__=='__main__':
  39.  
    pytest.main(['-rs','test01.py'])
  40.  
     
  41.  
     
  42.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  43.  
    ============================= test session starts =============================
  44.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  45.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  46.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  47.  
    collected 2 items
  48.  
     
  49.  
    test01.py s. [100%]
  50.  
     
  51.  
    =========================== short test summary info ===========================
  52.  
    SKIPPED [1] test01.py:414: 跳过一个方法或一个测试用例
  53.  
    ======================== 1 passed, 1 skipped in 0.05s =========================
  54.  
     
  55.  
    Process finished with exit code 0
  56.  
     
  57.  
    3、当条件满足,跳过方法或用例,备注了原因
  58.  
    import pytest,sys
  59.  
    class Test(object):
  60.  
    @pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例')
  61.  
    def test_one(self):
  62.  
    assert 1==2
  63.  
    def test_two(self):
  64.  
    print('test_02')
  65.  
    assert 1==1
  66.  
    if __name__=='__main__':
  67.  
    pytest.main(['-rs','test01.py'])
  68.  
     
  69.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  70.  
    ============================= test session starts =============================
  71.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  72.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  73.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  74.  
    collected 2 items
  75.  
     
  76.  
    test01.py s. [100%]
  77.  
     
  78.  
    =========================== short test summary info ===========================
  79.  
    SKIPPED [1] test01.py:414: 跳过一个方法或一个测试用例
  80.  
    ======================== 1 passed, 1 skipped in 0.06s =========================
  81.  
     
  82.  
    Process finished with exit code 0

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

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

  1.  
    import pytest,sys
  2.  
    @pytest.mark.skipif(1==1,reason='多个条件时,有1个条件满足就跳过(类)')
  3.  
    class Test(object):
  4.  
    @pytest.mark.skipif(1==2, reason='多个条件时,有1个条件满足就跳过(方法)')
  5.  
    def test_one(self):
  6.  
    assert 1==2
  7.  
    def test_two(self):
  8.  
    print('test_02')
  9.  
    assert 1==1
  10.  
    if __name__=='__main__':
  11.  
    pytest.main(['-rs','test01.py'])
  12.  
     
  13.  
     
  14.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  15.  
    ============================= test session starts =============================
  16.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  17.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  18.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  19.  
    collected 2 items
  20.  
     
  21.  
    test01.py ss [100%]
  22.  
     
  23.  
    =========================== short test summary info ===========================
  24.  
    SKIPPED [1] test01.py:418: 多个条件时,有1个条件满足就跳过(类)
  25.  
    SKIPPED [1] test01.py:415: 多个条件时,有1个条件满足就跳过(类)
  26.  
    ============================= 2 skipped in 0.04s ==============================

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

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

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

调用:@myskip

  1.  
    import pytest,sys
  2.  
    myskip=pytest.mark.skipif(1==1,reason='skip赋值给变量,可多处调用')
  3.  
    class Test(object):
  4.  
    @myskip
  5.  
    def test_one(self):
  6.  
    assert 1==2
  7.  
    def test_two(self):
  8.  
    print('test_02')
  9.  
    assert 1==1
  10.  
    if __name__=='__main__':
  11.  
    pytest.main(['-rs','test01.py'])
  12.  
     
  13.  
     
  14.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  15.  
    ============================= test session starts =============================
  16.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  17.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  18.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  19.  
    collected 2 items
  20.  
     
  21.  
    test01.py s. [100%]
  22.  
     
  23.  
    =========================== short test summary info ===========================
  24.  
    SKIPPED [1] test01.py:415: skip赋值给变量,可多处调用
  25.  
    ======================== 1 passed, 1 skipped in 0.07s =========================
  26.  
     
  27.  
    Process finished with exit code 0

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

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

  1.  
    import pytest,sys
  2.  
    class Test(object):
  3.  
    def test_one(self):
  4.  
    pytest.skip(msg='跳过')
  5.  
    assert 1==2
  6.  
    def test_two(self):
  7.  
    print('test_02')
  8.  
    assert 1==1
  9.  
    if __name__=='__main__':
  10.  
    pytest.main(['-rs','test01.py'])
  11.  
     
  12.  
     
  13.  
    "C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test01.py
  14.  
    ============================= test session starts =============================
  15.  
    platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
  16.  
    rootdir: C:\Users\wangli\PycharmProjects\Test\test
  17.  
    plugins: allure-pytest-2.8.5, html-1.22.0, metadata-1.8.0
  18.  
    collected 2 items
  19.  
     
  20.  
    test01.py s. [100%]
  21.  
     
  22.  
    =========================== short test summary info ===========================
  23.  
    SKIPPED [1] c:\users\wangli\pycharmprojects\test\test\test01.py:416: 跳过
  24.  
    ======================== 1 passed, 1 skipped in 0.04s =========================
  25.  
     
  26.  
    Process finished with exit code 0

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjbhgc
系列文章
更多 icon
同类精品
更多 icon
继续加载