标签:comm oca test utf-8 ecs ati pwd elf setup
一、
封装登录接口并进行参数化--禅道登录为例
login函数封装了登录接口并对登录账号和密码进行参数化
is_login_sucess函数封装了判断登录成功的方法
1 #testLogin.py 2 import requests 3 4 #登录案例 5 6 def login(s,admin,pwd): 7 s = requests.session() 8 url = "http://127.0.0.1/zentao/user-login-L3plbnRhby8=.html" 9 headers ={ 10 "Content-Type": "application/x-www-form-urlencoded" 11 } 12 body = { 13 "account":admin, 14 "password":pwd, 15 "referer":"http://127.0.0.1/zentao/my/", 16 "keepLogin[]": "on" 17 } 18 19 r = s.post(url,headers=headers,data=body,verify=False) 20 # print(r.content.decode(‘utf-8‘)) 21 return r.content.decode(‘utf-8‘) 22 23 def is_login_sucess(loginRes): 24 ‘‘‘ 25 判断是否登录成功 26 :param login:登录函数返回的内容 27 :return: Ture or False 28 ‘‘‘ 29 if "登录失败" in loginRes: 30 print("登录失败") 31 return False 32 elif "parent.location=" in loginRes: 33 print("登录成功") 34 return True 35 else: 36 print("出现异常,登录失败") 37 return False 38 39 if __name__ == ‘__main__‘: 40 s = requests.session() 41 loginRes = login(s,‘admin‘,‘e10adc3949ba59abbe56e057f20f883e‘) 42 result = is_login_sucess(loginRes) 43 print(result)
二、unittest设计测试用例
1 import unittest 2 import requests 3 from common.testLogin import login,is_login_sucess#导入包、方法 4 5 class TestCaseCSH(unittest.TestCase): 6 ‘‘‘禅道登录‘‘‘ 7 @classmethod 8 def setUpClass(cls): 9 cls.s = requests.session() 10 11 def tearDown(self): 12 self.s.cookies.clear() 13 # def setUp(self): 14 # self.s = requests.session() 15 # 16 # def tearDown(self): 17 # self.s.close() 18 19 def testlogin01(self): 20 ‘‘‘测试用例01‘‘‘ 21 res = login(self.s,"admin","e10adc3949ba59abbe56e057f20f883e") 22 result = is_login_sucess(res) 23 #期望结果,登录成功True 24 self.assertTrue(result) 25 26 27 def testlogin02(self): 28 ‘‘‘测试用例02‘‘‘ 29 res = login(self.s,"dmin","e10adc3949ba59abbe56e057f20f883e") 30 result = is_login_sucess(res) 31 #期望结果,登录失败False 32 self.assertFalse(result) 33 34 35 if __name__ == ‘__main__‘: 36 unittest.main()
python接口自动化--封装和参数化及unittest用例
标签:comm oca test utf-8 ecs ati pwd elf setup
原文地址:https://www.cnblogs.com/xiaoyujuan/p/11314933.html