自学接口自动化代码session的使用
2020-08-12 20:22
141 查看
自学接口自动化代码session的使用
# 导包 import unittest import requests # 创建集成unittest的框架的类 class TestTpshopLogin(unittest.TestCase): def setUp(self): # 实例化session self.session = requests.session() def tearDown(self): if self.session != None: self.session.close() # 编写实现登录的函数用例 def test01_login_success(self): # session = requests.session() # 使用session发送获取请求的验证码 response = self.session.get(url="http://localhost:80/index.php?m=home&c=user&a=verify") # 获取响应头 print("响应头:",response.headers) # 获取响应头content-type的值 print("获取响应头content-type的值:",response.headers.get("Content-Type")) # 断言响应的状态码 self.assertEqual("image/png",response.headers.get("Content-Type")) # 使用session登录 response = self.session.post(url="http://localhost:80/index.php?m=Home&c=User&a=do_login", headers={"Content-Type":"application/x-www-form-urlencoded"}, data="username=13800138006&password=123456&verify_code=8888") print("登录的结果:",response.json()) # 断言响应的状态码 self.assertEqual(200,response.status_code) # 断言status的值 self.assertEqual(1,response.json().get("status")) # 断言msg self.assertIn("登陆成功",response.json().get("msg")) # 关闭session # session.close() def test02_username_is_not_exist(self): # session = requests.session() # 使用session发送获取请求的验证码 response = self.session.get(url="http://localhost:80/index.php?m=home&c=user&a=verify") # 获取响应头 print("响应头:", response.headers) # 获取响应头content-type的值 print("获取响应头content-type的值:", response.headers.get("Content-Type")) # 断言响应的状态码 self.assertEqual("image/png", response.headers.get("Content-Type")) # 使用session登录 response = self.session.post(url="http://localhost:80/index.php?m=Home&c=User&a=do_login", headers={"Content-Type": "application/x-www-form-urlencoded"}, data="username=13800138106&password=123456&verify_code=8888") print("登录的结果:", response.json()) # 断言响应的状态码 self.assertEqual(200, response.status_code) # 断言status的值 self.assertEqual(-1, response.json().get("status")) # 断言msg self.assertIn("账号不存在", response.json().get("msg")) # 关闭session # session.close() def test03_password_error(self): # session = requests.session() # 使用session发送获取请求的验证码 response = self.session.get(url="http://localhost:80/index.php?m=home&c=user&a=verify") # 获取响应头 print("响应头:", response.headers) # 获取响应头content-type的值 print("获取响应头content-type的值:", response.headers.get("Content-Type")) # 断言响应的状态码 self.assertEqual("image/png", response.headers.get("Content-Type")) # 使用session登录 response = self.session.post(url="http://localhost:80/index.php?m=Home&c=User&a=do_login", headers={"Content-Type": "application/x-www-form-urlencoded"}, data="username=13800138006&password=12345&verify_code=8888") print("登录的结果:", response.json()) # 断言响应的状态码 self.assertEqual(200, response.status_code) # 断言status的值 self.assertEqual(-2, response.json().get("status")) # 断言msg self.assertIn("密码错误", response.json().get("msg")) # 关闭session # session.close()
相关文章推荐
- 自学接口自动化cookie的使用
- 演示Session对象使用 范例(示例代码下载)
- 优化使用mysql存储session的php代码
- mybatis SqlSessionDaoSupport的使用(附代码下载)
- 演示Session对象使用 范例(示例代码下载)
- PHP下使用CURL方式POST数据至API接口的代码
- 使用Fitnesse进行接口自动化测试
- 编写高质量代码改善C#程序的157个建议——建议42:使用泛型参数兼容泛型接口的不可变性
- VC中用Ado接口代码连接和使用与数据库
- 使用Doxygen来自动化生成项目代码文档
- vc动态遍历word中的书签名(使用自动化接口)
- 避免代码冗余,使用接口和泛型重构Java代码
- 避免代码冗余,使用接口和泛型重构Java代码
- SqlServer 2000 LinkSever 建立,出现 该访问接口支持该接口,但使用该接口时返回了失败代码。
- 使用HttpSessionListener接口监听Session的创建和失效
- hibernate4中使用Session doWork()方法进行jdbc操作(代码)
- 使用VS自动化模型代码片段 Get DTE and UIService
- 使用 Visual Studio .NET 客户端执行自动化功能后不退出 Office 应用程序 代码
- 《COM技术内幕》代码之 接口的实现,QueryInterface的实现,QueryInterface的使用
- 在MFC中使用自动化接口操作WORD