ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest(十五)--用例a失败,跳过测试用例b和c并标记失败xfail

2020-07-30 16:00:29  阅读:262  来源: 互联网

标签:登录 用例 -- xfail pytest 失败 test login


前言

当用例a失败的时候,如果用例b和用例c都是依赖于第一个用例的结果,那可以直接跳过用例b和c的测试,直接给他标记失败xfail

用到的场景,登录时第一个用例,登录之后的操作b是第二个用例,登录之后操作c是第三个用例,很明显三个用例都会走到登录。

如果登录失败了,那后面2个用例就没有必要了,直接跳过,并且标记为失败用例,这样可以节省用例时间。

用例设计

1.pytest里面用xfail标记用例为失败的用例,可以直接跳过。实现基本思路

  把登录写为前置操作

  对登录的账号和密码参数化,参数用data=[{"user":"admin","pwd":"123456"}]表示

  多个用例放到一个Test_xx的class里

  test_1,test_2,test_3全部调用fixture里面的login功能

  test_1测试登录用例

  test_2和test_3执行前用if判断登录的结果,登录失败就执行,pytest.xfail("登录不成功,标记为xfail")

 

#test_fix1.py
# coding:utf-8
import pytest
data=[{"user":"admin","pwd":"123456"}]
@pytest.fixture(scope="module")
def login(request):
    user=request.param["user"]
    pwd=request.param["pwd"]
    print("正在操作登录,账号:{},密码:{}".format(user,pwd))
    if pwd:
        return True
    else:
        return False
@pytest.mark.parametrize("login",data,indirect=True)
class Test__xx:
    def test_1(self,login):
        r=login
        print("用例1,登录结果:{}".format(r))
        assert r==True
    def test_2(self,login):
        r=login
        print("用例2,登录结果:{}".format(r))
        if not r:
            pytest.xfail("登录不成功,标记为xfail")
    def test_3(self,login):
        r=login
        print("用例3,登录结果:{}".format(r))
        if not r:
            pytest.xfail("登录不成功,标记为xfail")
        assert 1 == 1
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

 

三个用例全部通过

 

 标记为xfail

1.下面是登录失败情况的用例,修改data数据

#test_fix1.py
# coding:utf-8
import pytest
data=[{"user":"admin","pwd":""}]
@pytest.fixture(scope="module")
def login(request):
    user=request.param["user"]
    pwd=request.param["pwd"]
    print("正在操作登录,账号:{},密码:{}".format(user,pwd))
    if pwd:
        return True
    else:
        return False
@pytest.mark.parametrize("login",data,indirect=True)
class Test__xx:
    def test_1(self,login):
        r=login
        print("用例1,登录结果:{}".format(r))
        assert r==True
    def test_2(self,login):
        r=login
        print("用例2,登录结果:{}".format(r))
        if not r:
            pytest.xfail("登录不成功,标记为xfail")
    def test_3(self,login):
        r=login
        print("用例3,登录结果:{}".format(r))
        if not r:
            pytest.xfail("登录不成功,标记为xfail")
        assert 1 == 1
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"]) 

运行结果

test_fix1.py 正在操作登录,账号:admin,密码:
F用例1,登录结果:False

test_fix1.py:15 (Test__xx.test_1[login0])
False != True

Expected :True
Actual   :False
<Click to see difference>

self = <test_fix1.Test__xx object at 0x00000214306097B8>, login = False

    def test_1(self,login):
        r=login
        print("用例1,登录结果:{}".format(r))
>       assert r==True
E       assert False == True

test_fix1.py:19: AssertionError
x用例2,登录结果:False

self = <test_fix1.Test__xx object at 0x0000021430603DD8>, login = False

    def test_2(self,login):
        r=login
        print("用例2,登录结果:{}".format(r))
        if not r:
>           pytest.xfail("登录不成功,标记为xfail")
E           _pytest.outcomes.XFailed: 登录不成功,标记为xfail

test_fix1.py:24: XFailed
x用例3,登录结果:False

self = <test_fix1.Test__xx object at 0x0000021430609710>, login = False

    def test_3(self,login):
        r=login
        print("用例3,登录结果:{}".format(r))
        if not r:
>           pytest.xfail("登录不成功,标记为xfail")
E           _pytest.outcomes.XFailed: 登录不成功,标记为xfail

test_fix1.py:29: XFailed
                                                         [100%]

================================== FAILURES ===================================
___________________________ Test__xx.test_1[login0] ___________________________

self = <test_fix1.Test__xx object at 0x00000214306097B8>, login = False

    def test_1(self,login):
        r=login
        print("用例1,登录结果:{}".format(r))
>       assert r==True
E       assert False == True

test_fix1.py:19: AssertionError
---------------------------- Captured stdout setup ----------------------------
正在操作登录,账号:admin,密码:
---------------------------- Captured stdout call -----------------------------
用例1,登录结果:False
=========================== short test summary info ===========================
FAILED test_fix1.py::Test__xx::test_1[login0] - assert False == True
======================== 1 failed, 2 xfailed in 0.04s =========================

 

标签:登录,用例,--,xfail,pytest,失败,test,login
来源: https://www.cnblogs.com/canglongdao/p/13404309.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有