接口测试 python requests库

requests参数

  1. params:在GET请求中,将参数添加到URL的查询字符串中。
  2. data:在POST请求中,将参数添加到请求体中。
  3. headers:设置HTTP请求的头部信息,如User-Agent、Content-Type等。
  4. cookies:设置请求的Cookies。
  5. auth:设置HTTP认证信息,如用户名和密码。
  6. timeout:设置请求超时时间。
  7. proxies:设置代理服务器。
  8. verify:是否验证服务器的SSL证书。
  9. allow_redirects:是否允许重定向。

发送GET请求

import requests

'''
发送GET请求,解决响应乱码问题
'''
response = requests.get("http://www.baidu.com/")

print("原始的数据编码为:", response.encoding)
print("设置前响应数据:", response.text)
# 设置响应数据编码格式
# 响应数据乱码,设置编码
response.encoding = "utf-8"
print("设置编码后数据编码为:", response.encoding)
print("设置后响应数据:", response.text)

发送POST请求

提交form表单,参数为data

import requests
login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
    "username": "13641111112",
    "password": "123456",
    "verify_code": "8888"
}
response = requests.post(url=login_url, data=login_data)
# 看响应
print(response.json())

提交json数据,参数为json

import requests
# 发送请求
login_url = "http://ihrm-java.itheima.net/api/sys/login"
login_data = {
    "mobile": "13800000002",
    "password": "123456"
}
response = requests.post(url=login_url, json=login_data)
# 查看响应
print(response.json())

传递URL参数

提交url参数数据,参数为params

import requests
# 方式一:直接定义在URL中
# response = requests.get("http://localhost/Home/Goods/search.html?q=iPhone")
# 方式二:传递字符串类型的参数
# response = requests.get("http://localhost/Home/Goods/search.html", params="q=iPhone")
# 方式三:传递字典类型的参数
response = requests.get("http://localhost/Home/Goods/search.html", params={"q": "iPhone"
})
# 获取响应结果
print("url=", response.url)

响应内容解析

import requests

# 1). 访问百度首页的接口`http://www.baidu.com`,获取以下响应数据
response = requests.get("http://www.baidu.com")
# 2). 获取响应状态码
print("响应状态码:", response.status_code)
# 3). 获取请求URL
print("URL:", response.url)
# 4). 获取响应字符编码
print("编码格式为:", response.encoding)
# 5). 获取响应头数据
print("响应头信息:", response.headers)
print("Content-Type:", response.headers.get("Content-Type"))
# 6). 获取响应的cookie数据
print("cookies:", response.cookies)
print("提取指定的cookie:", response.cookies.get("BDORZ"))
# 7). 获取文本形式的响应内容
print("文本形式显示响应内容:", response.text)
# 8). 获取字节形式的响应内容
print("获取字节形式的响应内容:", response.content)
print("获取字节形式的响应内容:", response.content.decode("utf-8"))

设置请求头

参数为headers

import requests

login_url = "http://ihrm-java.itheima.net/api/sys/login"
login_header = {
    "Content-Type": "application/json"
}
login_data = {
    "mobile": "13800000002",
    "password": "123456"
}
# 发送请求
response = requests.post(url=login_url, json=login_data, headers=login_header)
# 查看响应
print(response.json())

设置cookie

作用:跳过登录验证

参数为cookies

1.从登录状态中获取cookie

2.在代码中设置

3.请求时带上cookies参数

import requests

# 获取验证码
response = requests.get("http://localhost/index.php?m=Home&c=User&a=verify")
print(response.cookies)
PHPSESSID = response.cookies.get("PHPSESSID")
print(PHPSESSID)
# 登录
login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
    "username": "13641111112",
    "password": "123456",
    "verify_code": "8888"
}
cookies = {
    "PHPSESSID": PHPSESSID
}
response = requests.post(url=login_url, data=login_data, cookies=cookies)
response.encoding = "utf-8"
print(response.json())
response = requests.get("http://localhost/index.php/Home/Order/order_list.html",
                        cookies=cookies)
response.encoding = "utf-8"
print(response.text)

设置session

作用:保持会话

import requests
# 创建session对象
session = requests.Session()
# 获取验证码
response = session.get("http://localhost/index.php?m=Home&c=User&a=verify")
# 登录
login_url = "http://localhost/index.php?m=Home&c=User&a=do_login"
login_data = {
    "username": "13641111112",
    "password": "123456",
    "verify_code": "8888"
}
response = session.post(url=login_url, data=login_data)
print(response.json())
# 我的订单页
response = session.get("http://localhost/index.php/Home/Order/order_list.html")
response.encoding = "utf-8"
print(response.text)

接口测试框架

# 导包
import requests
import unittest


# 创建测试类
class TPShopLogin(unittest.TestCase):

    def setUp(self):
        # 实例化session对象
        self.session = requests.Session()
        # 定义验证接口url地址
        self.url_verify = "http://localhost/index.php?m=Home&c=User&a=verify"
        # 定义登录接口url地址
        self.url_login = "http://localhost/index.php?m=Home&c=User&a=do_login"

    # teardown
    def tearDown(self):
        # 关闭session对象
        self.session.close()

    # 登录成功
    def test01_success(self):
        # 发送验证码请求并断言
        response = self.session.get(url=self.url_verify)
        self.assertEqual(200, response.status_code)
        self.assertIn("image", response.headers.get("Content-Type"))
        # 发登录请求并断言
        login_data = {
            "username": "13641111112",
            "password": "123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, response.json().get("status"))
        self.assertIn("登陆成功", response.json().get("msg"))

    # 账号不存在
    def test02_user_is_not_exist(self):
        # 发送验证码请求并断言
        response = self.session.get(url=self.url_verify)
        self.assertEqual(200, response.status_code)
        self.assertIn("image", response.headers.get("Content-Type"))
        # 发登录请求并断言
        login_data = {
            "username": "13641111111",
            "password": "123456",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-1, response.json().get("status"))
        self.assertIn("账号不存在", response.json().get("msg"))

    # 密码错误
    def test03_password_error(self):
        # 发送验证码请求并断言
        response = self.session.get(url=self.url_verify)
        self.assertEqual(200, response.status_code)
        self.assertIn("image", response.headers.get("Content-Type"))
        # 发登录请求并断言
        login_data = {
            "username": "13641111112",
            "password": "error",
            "verify_code": "8888"
        }
        response = self.session.post(url=self.url_login, data=login_data)
        print(response.json())
        self.assertEqual(200, response.status_code)
        self.assertEqual(-2, response.json().get("status"))
        self.assertIn("密码错误", response.json().get("msg"))

接口测试

'''
成功登录后,记录登录信息
查询用户信息
'''
# 导包
import requests
import unittest


# 创建测试类
class TPShopLogin(unittest.TestCase):

    def setUp(self):
        # 实例化session对象
        self.session = requests.Session()
        # 定义接口url地址
        self.base_url = "http://ihrm-java.itheima.net/"

    # teardown
    def tearDown(self):
        # 关闭session对象
        self.session.close()

    # 登录成功并查询用户
    def test01_success(self):
        login_data = {
            "mobile": "13800000002",
            "password": "888itcast.CN764%..."
        }
        headers = {"Content-Type": "application/json"}
        response = self.session.post(url=self.base_url + '/api/sys/login', json=login_data, headers=headers)
        print(response.json())
        print(response.json().get("data"))
        self.assertEqual(200, response.status_code)
        data = response.json().get("data")
        header = {"Content-Type": "application/json", "Authorization": "Bearer " + data}
        response = self.session.post(url=self.base_url + '/api/sys/profile', headers=header)
        print(response.json())