概述
前面一章讲解了单个接口的测试,但是实际项目中,因为权限和登录状态的限制,大部分接口没办法直接访问到,这时候我们想访问到一个系统的接口,就需要模拟用户登录拿到用户的token和所拥有的权限之后再将这些信息作为参数传递给下游进行测试
举个栗子:
base_url : 定义接口的基础 URL。所有需要这个 URL 的测试函数都可以使用这个 fixture。
@pytest.fixture
def base_url():
return "http://localhost:8081"
请求访问登录接口:
参数封装:
@pytest.fixture
def login_payload():
return {
"username": "admin",
"password": "123",
"captcha": "12",
"uuid": "10f53b80-d83f-4431-8fb2-09280942522a"
}
之后请求访问登录页面
@pytest.fixture
def token(base_url, login_payload):
response = requests.post(f"{base_url}/user/login", json=login_payload)
assert response.status_code == 200
data = response.json()
assert data["code"] == "200"
token = data["data"]["token"]
return token
这样我们就能拿到用户的token信息
将用户的请求头信息按照系统存放的规则存入请求头中
def test_course_page(base_url, token):
headers = {
"Token": token
}
params = {
"pageNum": 1,
"pageSize": 10,
"name": "微表情",
"teacherName": "",
"courseModule": ""
}
response = requests.get(f"{base_url}/course/page", headers=headers, params=params)
# 打印响应详细信息
print("Status Code:", response.status_code)
print("Headers:", response.headers)
print("Response Text:", response.text)
# 断言部分
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/json"
data = response.json()
# 检查响应中的具体字段
assert data["code"] == "200"
assert "msg" in data
assert "data" in data
course_data = data["data"]
assert "total" in course_data
assert "data" in course_data
assert len(course_data["data"]) > 0
# 检查第一个课程的具体字段
first_course = course_data["data"][0]
assert "id" in first_course
assert "name" in first_course
assert first_course["name"] == "微表情专业课"
全部代码实例如下:
import requests
import pytest
@pytest.fixture
def base_url():
return "http://localhost:8081"
@pytest.fixture
def login_payload():
return {
"username": "admin",
"password": "123",
"captcha": "12",
"uuid": "10f53b80-d83f-4431-8fb2-09280942522a"
}
@pytest.fixture
def token(base_url, login_payload):
response = requests.post(f"{base_url}/user/login", json=login_payload)
assert response.status_code == 200
data = response.json()
assert data["code"] == "200"
token = data["data"]["token"]
return token
def test_course_page(base_url, token):
headers = {
"Token": token
}
params = {
"pageNum": 1,
"pageSize": 10,
"name": "微表情",
"teacherName": "",
"courseModule": ""
}
response = requests.get(f"{base_url}/course/page", headers=headers, params=params)
# 打印响应详细信息
print("Status Code:", response.status_code)
print("Headers:", response.headers)
print("Response Text:", response.text)
# 断言部分
assert response.status_code == 200
assert response.headers["Content-Type"] == "application/json"
data = response.json()
# 检查响应中的具体字段
assert data["code"] == "200"
assert "msg" in data
assert "data" in data
course_data = data["data"]
assert "total" in course_data
assert "data" in course_data
assert len(course_data["data"]) > 0
# 检查第一个课程的具体字段
first_course = course_data["data"][0]
assert "id" in first_course
assert "name" in first_course
assert first_course["name"] == "微表情专业课"
# 运行测试
if __name__ == "__main__":
pytest.main(["-s", "test_course_page.py"])
运行结果:
更新参数,模拟登录失败的情况
类似的我们可以将参数改为可以动态录入,可以将其定义为函数的参数,并通过函数调用传递不同的参数值。这样可以使测试更加灵活,并且可以轻松地重复执行相同的测试用例以覆盖不同的情况。