【pytest-15】用例a失败,其他关联用例标记为失败xfail
前言
在实际工作场景中,恐怕只有这种登录用例a失败,关联的下个用例b,c也会失败;如果a失败,直接将b,c用例标记为失败
如果登录a都失败,显然b,c都执行不了,这种直接标记为失败,就不用测试了,直接跳过。
用例设计
- pytest里面用xfail标记为失败的用例,可以直接跳过,实现基本思路
- 把登录写为前置操作
- 对登录的账户和密码参数化,参数用data=[{"user":"admin","pwd":"admin"}]表示
示例:这里以登录,搜索,增加用例为例子
#encoding:utf-8 #@Time:2020/12/11 10:29 #@Author:sunny import pytest data=[{"user":"admin","pwd":"admin"}] @pytest.fixture(scope="module") def login(request): user=request.param['user'] pwd=request.param['pwd'] print("登录用户名:%s" % user) print("登录密码:%s" % pwd) if pwd: return True else: return False @pytest.mark.parametrize("login",data,indirect=True) class TestCalss: def test_login(self,login): '''登录''' result=login print("登录用例的结果:%s"%result) assert result==True def test_search(self,login): '''搜索''' result=login print("搜索用例的结果:%s" % result) assert result == True def test_add(self,login): '''增加''' result=login print("增加用例的结果:%s" % result) assert result == True if __name__ == '__main__': pytest.main(["-s","test_login.py"])
运行结果:
Testing started at 14:13 ...============================= test session starts =============================collected 3 items test_login.py 登录用户名:admin 登录密码:admin .登录用例的结果:True .搜索用例的结果:True .增加用例的结果:True [100%] ============================== 3 passed in 0.02s ============================== Process finished with exit code 0
通过看运行结果,是成功运行的
标记为xfail
在看看登录失败情况的用例,修改登录的参数
#encoding:utf-8 #@Time:2020/12/11 10:29 #@Author:sunny import pytest data=[{"user":"admin","pwd":""}] @pytest.fixture(scope="module") def login(request): user=request.param['user'] pwd=request.param['pwd'] print("登录用户名:%s" % user) print("登录密码:%s" % pwd) if pwd: return True else: return False @pytest.mark.parametrize("login",data,indirect=True) class TestCalss: def test_login(self,login): '''登录''' result=login print("登录用例的结果:%s"%result) assert result==True def test_search(self,login): '''搜索''' result=login print("搜索用例的结果:%s" % result) # assert result == True if not result: pytest.xfail("登录不成功,标记为xfail") def test_add(self,login): '''增加''' result=login print("增加用例的结果:%s" % result) # assert result == True if not result: pytest.xfail("登录不成功,标记为xfail") if __name__ == '__main__': pytest.main(["-s","test_login.py"])
运行结果:
Testing started at 14:23 ...============================= test session starts =============================collected 3 items test_login.py 登录用户名:admin 登录密码: F登录用例的结果:False test_login.py:21 (TestCalss.test_login[login0]) True != False Expected :False Actual :True <Click to see difference> self = <common.test_login.TestCalss object at 0x0000022CD1DDB6D8>, login = False def test_login(self,login): '''登录''' result=login print("登录用例的结果:%s"%result) > assert result==True E assert False == True test_login.py:26: AssertionError x搜索用例的结果:False self = <common.test_login.TestCalss object at 0x0000022CD1DE5780>, login = False def test_search(self,login): '''搜索''' result=login print("搜索用例的结果:%s" % result) # assert result == True if not result: > pytest.xfail("登录不成功,标记为xfail") E _pytest.outcomes.XFailed: 登录不成功,标记为xfail test_login.py:34: XFailed x增加用例的结果:False self = <common.test_login.TestCalss object at 0x0000022CD1DD9A90>, login = False def test_add(self,login): '''增加''' result=login print("增加用例的结果:%s" % result) # assert result == True if not result: > pytest.xfail("登录不成功,标记为xfail") E _pytest.outcomes.XFailed: 登录不成功,标记为xfail test_login.py:42: XFailed [100%] ================================== FAILURES =================================== ________________________ TestCalss.test_login[login0] _________________________ self = <common.test_login.TestCalss object at 0x0000022CD1DDB6D8>, login = False def test_login(self,login): '''登录''' result=login print("登录用例的结果:%s"%result) > assert result==True E assert False == True test_login.py:26: AssertionError ---------------------------- Captured stdout setup ---------------------------- 登录用户名:admin 登录密码: ---------------------------- Captured stdout call ----------------------------- 登录用例的结果:False =========================== short test summary info =========================== FAILED test_login.py::TestCalss::test_login[login0] - assert False == True ======================== 1 failed, 2 xfailed in 0.07s ========================= Process finished with exit code 0
从运行结果可以看出,登录用例失败了,搜索用例和增加用例没执行,直接标记为xfail了。
善于跌倒仍喜爱奔跑~

浙公网安备 33010602011771号