您的位置:首页 > 编程语言

自学接口自动化代码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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: