前言
当用例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"
test_login.py
import pytest import requests s = requests.session() canshu = [{'user':'xxxx', 'paw':111111, 'uri':'xxxxxxxxxx'}] @pytest.fixture(scope='module') def login(request): user = request.param['user'] paw = request.param['paw'] uri = request.param['uri'] url = uri + "/global/do-login.action" body = { "loginName": "%s" % user, "password": "%s" % paw, "pcCodeForFocusMedia": 0 } a = s.post(url, data=body, verify=False) data = a.json() print(data['status']) if data['status'] == 202: return True else: return False
test_parametrize.py
import pytest, requests, urllib3 from s14.pytest_learn import test_login urllib3.disable_warnings() canshu = test_login.canshu login = test_login.login tenantId = xxx # 租户ID encryptionKey = "xxxxxxxxx" # 保持登陆的key passport_id = xxxxx # 跟据账号进行填写,同一个账户下的多个租户,passport.id不用更改 uri = 'xxxx' user = 'xxxx' @pytest.mark.parametrize('login', canshu, indirect=True) class Test_xxxx(): s = requests.session() def test_01(self, login): '''登陆''' result = login print("用例1: %s" % result) assert result == True def test_02(self, login): result = login print("用例2: %s" % result) url1 = uri + "/global/login-entry.action" body1 = { "tenantId": "%s" % tenantId, "passport.id": passport_id, "encryptionKey": "%s" % encryptionKey, "loginName": "%s" % user } b = self.s.post(url1, data=body1, verify=False) data = b.json() a = data['status'] if not result: pytest.xfail("登陆不成功,标记为xfail") else: print("用例2运行结果为 :True , data['status']状态为:%s" % a) assert data['status'] == 0 if __name__ == '__main__': pytest.main(['-s', 'test_parametrize.py'])
上面传的登录参数是登录成功的案例,两个用例全部通过
============================= test session starts ============================= platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1 rootdir: D:python_autos14pytest_learn, inifile: collected 2 items test_parametrize.py 202 用例1: True .用例2: True 用例2运行结果为 :True , data['status']状态为:0 . ========================== 2 passed in 0.55 seconds ===========================
标记为xfail
1.再看看登录失败情况的用例,修改登录的参数
import pytest import requests s = requests.session() canshu = [{'user':'xxxxx', 'paw':'', 'uri':'xxxxxx'}] @pytest.fixture(scope='module') def login(request): user = request.param['user'] paw = request.param['paw'] uri = request.param['uri'] url = uri + "/global/do-login.action" body = { "loginName": "%s" % user, "password": "%s" % paw, "pcCodeForFocusMedia": 0 } a = s.post(url, data=body, verify=False) data = a.json() print(data['status']) if data['status'] == 202: return True else: return False
test_parametrize.py
import pytest, requests, urllib3 from s14.pytest_learn import test_login urllib3.disable_warnings() canshu = test_login.canshu login = test_login.login tenantId = xxx # 租户ID encryptionKey = "xxxxxxxxx" # 保持登陆的key passport_id = xxxxx # 跟据账号进行填写,同一个账户下的多个租户,passport.id不用更改 uri = 'xxxx' user = 'xxxx' @pytest.mark.parametrize('login', canshu, indirect=True) class Test_xxxx(): s = requests.session() def test_01(self, login): '''登陆''' result = login print("用例1: %s" % result) assert result == True def test_02(self, login): result = login print("用例2: %s" % result) url1 = uri + "/global/login-entry.action" body1 = { "tenantId": "%s" % tenantId, "passport.id": passport_id, "encryptionKey": "%s" % encryptionKey, "loginName": "%s" % user } b = self.s.post(url1, data=body1, verify=False) data = b.json() a = data['status'] if not result: pytest.xfail("登陆不成功,标记为xfail") else: print("用例2运行结果为 :True , data['status']状态为:%s" % a) assert data['status'] == 0 if __name__ == '__main__': pytest.main(['-s', 'test_parametrize.py'])
运行结果
============================= test session starts ============================= platform win32 -- Python 3.6.2, pytest-3.7.4, py-1.6.0, pluggy-0.7.1 rootdir: D:python_autos14pytest_learn, inifile: collected 2 items test_parametrize.py 401 用例1: False F用例2: False x ================================== FAILURES =================================== __________________________ Test_xxxx.test_01[login0] __________________________ self = <test_parametrize.Test_xxxx object at 0x03A4C730>, login = False def test_01(self, login): '''登陆''' result = login print("用例1: %s" % result) > assert result == True E assert False == True test_parametrize.py:22: AssertionError ===================== 1 failed, 1 xfailed in 0.51 seconds =====================
从结果可以看出用例1失败了,用例2和3没执行,直接标记为xfail了
作者:含笑半步颠√
博客链接:https://www.cnblogs.com/lixy-88428977
声明:本文为博主学习感悟总结,水平有限,如果不当,欢迎指正。如果您认为还不错,欢迎转载。转载与引用请注明作者及出处。