zoukankan      html  css  js  c++  java
  • pytest十:用例 a 失败,跳过测试用例 b 和 c 并标记失败 xfail

    当用例 a 失败的时候,如果用例 b 和用例 c 都是依赖于第一个用例的结果,那可以直接跳过用例 b 和 c 的测试,直接给他标记失败 xfail
    用到的场景,登录是第一个用例,登录之后的操作 b 是第二个用例,登录之后操作 c 是第三个用例,很明显三个用例都会走到登录。
    如果登录都失败了,那后面 2 个用例就没测试必要了,直接跳过,并且标记为失败用例,这样可以节省用例时间。

    用例设计
    pytest 里面用 xfail 标记用例为失败的用例,可以直接跳过。实现基本思路
      把登录写为前置操作
      对登录的账户和密码参数化,参数用 canshu = [{"user":"amdin", "psw":"111"}]表示
      多个用例放到一个 Test_xx 的 class 里
      test_01,test_02, test_03 全部调用 fixture 里面的 login 功能
      test_01 测试登录用例
      test_02 和 test_03 执行前用 if 判断登录的结果,登录失败就执行,pytest.xfail("登录不成功, 标记为 xfail")

    import pytest

    canshu = [{'user': 'admin', 'psw': '111'}]

    @pytest.fixture(scope='module')
    def login(request):
    user = request.param['user']
    psw = request.param['psw']
    print(f' 正在操作登录,账号:{user},密码:{psw}')
    if psw:
    return True
    else:
    return False
    @pytest.mark.parametrize('login', canshu, indirect=True)
    class Test_xx():
    def test_01(self, login):
    '''用例1登录'''
    result = login
    print(f'用例1{result}')
    assert result == True

    def test_02(self, login):
    result = login
    print(f'用例2,登录结果:{result}')
    if not result:
    pytest.xfail('登录不成功,标记为xfail')
    assert 1 == 1

    def test_03(self, login):
    result = login
    print(f'用例3,登录结果:{result}')
    if not result:
    pytest.xfail('登录不成功,标记为xfail')
    assert 1 == 1

    if __name__=='__main__':
    pytest.main()

     

    上面传的登录参数是登录成功的案例,三个用例全部通过


    标记为 xfail
    再看看登录失败情况的用例,修改登录的参数

    从结果可以看出用例 1 失败了,用例 2 和 3 没执行,直接标记为xfail 了
    
    
    import pytest

    canshu = [{'user': 'admin', 'psw': ''}]

    @pytest.fixture(scope='module')
    def login(request):
    user = request.param['user']
    psw = request.param['psw']
    print(f' 正在操作登录,账号:{user},密码:{psw}')
    if psw:
    return True
    else:
    return False
    @pytest.mark.parametrize('login', canshu, indirect=True)
    class Test_xx():
    def test_01(self, login):
    '''用例1登录'''
    result = login
    print(f'用例1{result}')
    assert result == True

    def test_02(self, login):
    result = login
    print(f'用例2,登录结果:{result}')
    if not result:
    pytest.xfail('登录不成功,标记为xfail')
    assert 1 == 1

    def test_03(self, login):
    result = login
    print(f'用例3,登录结果:{result}')
    if not result:
    pytest.xfail('登录不成功,标记为xfail')
    assert 1 == 1

    if __name__=='__main__':
    pytest.main()










  • 相关阅读:
    .net百度编辑器的使用
    phpstudy远程连接mysql
    HDU-2389 Rain on your Parade
    HDU-2768 Cat vs. Dog
    HDU-1151 Air Raid
    HDU-1507 Uncle Tom's Inherited Land*
    HDU-1528/1962 Card Game Cheater
    HDU-3360 National Treasures
    HDU-2413 Against Mammoths
    HDU-1045 Fire Net
  • 原文地址:https://www.cnblogs.com/zhongyehai/p/9681971.html
Copyright © 2011-2022 走看看