前言
當用例a失敗的時候,如果用例b和用例c都是依賴於第一個用例的結果,那可以直接跳過用例b和c的測試,直接給他標記失敗xfail
用到的場景,登錄是第一個用例,登錄之后的操作b是第二個用例,登錄之后操作c是第三個用例,很明顯三個用例都會走到登錄。
如果登錄都失敗了,那后面2個用例就沒測試必要了,直接跳過,並且標記為失敗用例,這樣可以節省用例時間。
用例設計
1.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")
# content of test_05.py
# coding:utf-8
import pytest
# ** 作者:上海-悠悠 QQ交流群:588402570**
canshu = [{"user":"amdin", "psw":"111"}]
@pytest.fixture(scope="module")
def login(request):
user = request.param["user"]
psw = request.param["psw"]
print("正在操作登錄,賬號:%s, 密碼:%s" % (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("用例1:%s" % result)
assert result == True
def test_02(self, login):
result = login
print("用例3,登錄結果:%s" % result)
if not result:
pytest.xfail("登錄不成功, 標記為xfail")
assert 1 == 1
def test_03(self, login):
result = login
print("用例3,登錄結果:%s" %result)
if not result:
pytest.xfail("登錄不成功, 標記為xfail")
assert 1 == 1
if __name__ == "__main__":
pytest.main(["-s", "test_05.py"])
上面傳的登錄參數是登錄成功的案例,三個用例全部通過
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 3 items
..\..\..\..\..\..\YOYO\peizhi\test_05.py 正在操作登錄,賬號:amdin, 密碼:111
用例1:True
.用例3,登錄結果:True
.用例3,登錄結果:True
.
========================== 3 passed in 0.02 seconds ===========================
標記為xfail
1.再看看登錄失敗情況的用例,修改登錄的參數
# content of test_05.py
# coding:utf-8
import pytest
# ** 作者:上海-悠悠 QQ交流群:588402570**
canshu = [{"user":"amdin", "psw":""}]
@pytest.fixture(scope="module")
def login(request):
user = request.param["user"]
psw = request.param["psw"]
print("正在操作登錄,賬號:%s, 密碼:%s" % (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("用例1:%s" % result)
assert result == True
def test_02(self, login):
result = login
print("用例3,登錄結果:%s" % result)
if not result:
pytest.xfail("登錄不成功, 標記為xfail")
assert 1 == 1
def test_03(self, login):
result = login
print("用例3,登錄結果:%s" %result)
if not result:
pytest.xfail("登錄不成功, 標記為xfail")
assert 1 == 1
if __name__ == "__main__":
pytest.main(["-s", "test_05.py"])
運行結果
============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: D:\, inifile:
plugins: metadata-1.7.0, html-1.19.0, allure-adaptor-1.7.10
collected 3 items
..\..\..\..\..\..\YOYO\peizhi\test_05.py 正在操作登錄,賬號:amdin, 密碼:
用例1:False
F
self = <YOYO.peizhi.test_05.Test_xx object at 0x00000000045ACF98>, login = False
def test_01(self, login):
'''用例1登錄'''
result = login
print("用例1:%s" % result)
> assert result == True
E assert False == True
D:\YOYO\peizhi\test_05.py:24: AssertionError
用例3,登錄結果:False
x
Test ignored.用例3,登錄結果:False
x
Test ignored.
================================== FAILURES ===================================
___________________________ Test_xx.test_01[login0] ___________________________
self = <YOYO.peizhi.test_05.Test_xx object at 0x00000000045ACF98>, login = False
def test_01(self, login):
'''用例1登錄'''
result = login
print("用例1:%s" % result)
> assert result == True
E assert False == True
D:\YOYO\peizhi\test_05.py:24: AssertionError
===================== 1 failed, 2 xfailed in 0.06 seconds =====================
從結果可以看出用例1失敗了,用例2和3沒執行,直接標記為xfail了
---------------------------------pytest結合selenium自動化完整版-------------------------
全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b
作者:上海-悠悠 QQ交流群:874033608
也可以關注下我的個人公眾號:yoyoketang