1.前言
当用例a失败的时候,如果用例b和用例c都是依赖于第一个用例的结果,那可以直接跳过用例b和c的测试,直接给他标记失败xfail
用到的场景,登录是第一个用例,登录之后的操作b是第二个用例,登录之后操作c是第三个用例,很明显三个用例都会用到登录操作。
如果登录都失败了,那后面2个用例就没测试必要了,直接跳过,并且标记为失败用例,这样可以节省用例时间。
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")
# content of test_05.py
# coding:utf-8
import pytest
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 ===========================
3.标记为Xfail
1.再看看登录失败情况的用例,修改登录的参数
# content of test_05.py
# coding:utf-8
import pytest
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了
网友评论