其他分享
首页 > 其他分享> > pytest(十五)--用例a失败,跳过测试用例b和c并标记失败xfail

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

作者:互联网

前言

当用例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