zoukankan      html  css  js  c++  java
  • pytest---跳过用例

    前言

      执行自动化用例的过程中,遇到已知bug或者其他平台的执行内容,这个时候我们可以选择用跳过用例,在unittest中有无条件跳过,也有满足条件进行跳过,那么pytest中也存在跳过用例。

    skip

    前面介绍了mark的时候,知道mark属于标记用例,那么当mark.skip的时候就表示无条件进行跳过用例

    import pytest
    
    class Test01():
    
        @pytest.mark.skip()
        def test_01(self):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
        @pytest.mark.skip()
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过下图可以发现,我们执行的用例1和用例2均被跳过了。

    reason参数

    执行跳过用例过程中,可以进行加入reason参数进行对跳过的用例进行补充说明详细信息,以及跳过的原因内容

    import pytest
    data =  [{'user': "anjing",
             'pwd': "123",},
            {'user': "admin",
             "pwd": "123"},]
    
    @pytest.fixture(params=data)
    def login(request):
            yield request.param
    
    
    class Test01():
    
        @pytest.mark.skip(reason='该用例存在bug,请跳过!')
        def test_01(self):
            print('---用例01---')
    
    
        def test_02(self):
            print('---用例02---')
    
        @pytest.mark.skip(reason='该用例还没有编写完成,请跳过')
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过下图可以看出来,用例1和用例3进行了跳过,并补充了详细的说明

    函数中判断是否跳过

    举个例子,如果我们测试后台过程中,不同的账号登录,会有不同的权限,如果其中一个用户没有改权限测试此功能,也让跳过,这个时候我们就会通过pytest.skip的方法进行判断进行跳过

    import pytest
    data =  [{'user': "anjing",
             'pwd': "123",},
            {'user': "admin",
             "pwd": "123"},]
    
    @pytest.fixture(params=data)
    def login(request):
            yield request.param
    
    
    class Test01():
    
        def test_01(self,login):
            if login['user']=='anjing' and login['pwd']=='123':
                print('用户:%s登录成功'%login['user'])
                print('执行用例01')
            else:
                pytest.skip("用户:%s没有权限进行操作xx功能"%login['user'])
    
        def test_02(self):
            print('---用例02---')
    
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过执行结果发现,参数admin没有通过,然后进行了跳过。当然用例之间的判断不仅仅这样,只是简单的举个例子,具体的结合实际项目为准

    skipif

     skipif也是属于mark中的一个方法,也是跳过用例的内容,支持条件跳过,比如该用例只能在Windows上执行。 @pytest.mark.skipif(condition, reason='跳过的原因!') 

    其中condition表示跳过的条件,为True则跳过,reason和上述一样,表示跳过的自定义条件

    import pytest
    import sys
    
    
    class Test01():
    
        @pytest.mark.skipif(sys.platform=='win32', reason='在linux上运行,跳过Windows')
        def test_01(self):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过下图可以发现,安静在Windows上运行代码时,自动跳过了。注意:skipif必须要带上跳过的详细信息,要不然会报错

    整个类自动跳过

    skip和skipif不仅仅能够函数上进行添加装饰器,还可以直接在类上进行使用,直接跳过整个类。

    import pytest
    import sys
    
    @pytest.mark.skipif(sys.platform=='win32', reason='在linux上运行,跳过Windows')
    class Test01():
    
        def test_01(self):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    先来个图为skipif在类上进行跳过

    skip跳过类

    import pytest
    
    
    @pytest.mark.skip(reason='改功能还未实现,请跳过用例')
    class Test01():
    
        def test_01(self):
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
        def test_03(self):
            print('---用例03---')
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    xfail

    在执行自动化的时候,会有这种情况,知道该功能存在稳定性问题,不想要执行,但是也不想要跳过。这个时候我们就可以使用xfail,xfail表示测试期望是失败的,不会影响测试用例的执行,如果执行成功则报xpass,如果失败就会报xfail。

    import pytest
    
    class Test01:
    
        @pytest.mark.xfail()
        def test_01(self):
            print('---用例01---,该功能暂未实现!')
    
        def test_02(self):
            print('---用例02---')
    
        @pytest.mark.xfail()
        def test_03(self):
            print('---用例03---,预期用例失败!')
            assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过执行结果可以看出来,我们通过xfail标记的用例,已经全部执行,执行成功的显示xpass,失败的显示xfail,执行失败也没有继续报错。

     另一种写法:

    有的小伙伴们觉得,在用例前标记比较麻烦,能在用例中进行标记吗?当然是可以的。

    import pytest
    
    class Test01:
    
        def test_01(self):
            pytest.xfail(reason='该功能暂时未实现!')
            print('---用例01---')
    
        def test_02(self):
            print('---用例02---')
    
        def test_03(self):
            print('---用例03---,预期用例失败!')
            pytest.xfail(reason='该功能存在bug')
            assert 1==2
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过执行结果可以看到,test_01和test_03已经被预计失败了,细心的朋友会发现,pytest.xfail在用例中标记的时候,代码执行完这句话后就立即退出该用例,继续执行下一条用例。

    简单的介绍了pytest的跳过用例的方法,看了第一遍可能会觉得和unittest差不多,但是多看几遍,随便写的比较少,但是实用性很好,具体怎么用,就看大家在项目中怎么设计了。

     

  • 相关阅读:
    Android Studio代码自己主动检測错误提示
    uva 1567
    UWP 新手教程2——怎样实现自适应用户界面
    远程服务的使用场景
    本地服务和远程服务
    本地应用调用远程服务中的方法
    混合方式开启服务
    绑定服务抽取接口
    绑定服务调用服务里的方法
    bind绑定服务的生命周期
  • 原文地址:https://www.cnblogs.com/qican/p/14274267.html
Copyright © 2011-2022 走看看