编写下面这段需求的python自动化接口测试用例。需求说明: 1.手机号输入,限制只能输入数字且手机号限制输入11位,除数字外字符输入弹出提升“输入错误”;2.点击发送验证码,向服务端发起获取验证码请求,并且客户端提示”验证码已发送";3.用户密码输入,只能输入数字或字母,限制位数为8-16位;4.点击注册提交数据,5.用户协议默认选中,用户点击“隐私协议或“用户协议”跳转至对应协议页面;6.点击“直接登录”跳转至登录弹框; 7.当用户点击"X”按钮,关闭注册弹框
时间: 2024-03-28 12:38:10 浏览: 54
以下是一个示例自动化接口测试用例的代码:
```python
import requests
# 测试手机号输入
def test_phone_number_input():
# 输入正确的手机号
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901"})
assert response.status_code == 200
assert response.text == "验证码已发送"
# 输入错误的手机号(包含非数字字符)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "1234567890a"})
assert response.status_code == 400
assert response.text == "输入错误"
# 输入错误的手机号(长度不足11位)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "1234567890"})
assert response.status_code == 400
assert response.text == "输入错误"
# 输入错误的手机号(长度超过11位)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "123456789012"})
assert response.status_code == 400
assert response.text == "输入错误"
# 测试获取验证码
def test_send_verification_code():
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901"})
assert response.status_code == 200
assert response.text == "验证码已发送"
# 测试密码输入
def test_password_input():
# 输入正确的密码
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "password"})
assert response.status_code == 200
# 输入错误的密码(包含非数字或字母字符)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "password!"})
assert response.status_code == 400
# 输入错误的密码(长度不足8位)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "pass"})
assert response.status_code == 400
# 输入错误的密码(长度超过16位)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "passwordpassword"})
assert response.status_code == 400
# 测试注册
def test_registration():
# 提交正确的数据
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "password", "agree_to_terms": "true"})
assert response.status_code == 200
# 提交错误的数据(未同意用户协议)
response = requests.post(url="http://localhost:8000/register", data={"phone_number": "12345678901", "password": "password", "agree_to_terms": "false"})
assert response.status_code == 400
# 测试用户协议
def test_terms_of_service():
# 点击用户协议,跳转至协议页面
response = requests.get(url="http://localhost:8000/terms-of-service")
assert response.status_code == 200
assert "用户协议" in response.text
# 点击隐私协议,跳转至协议页面
response = requests.get(url="http://localhost:8000/privacy-policy")
assert response.status_code == 200
assert "隐私协议" in response.text
# 测试登录
def test_login():
# 点击直接登录,跳转至登录弹框
response = requests.get(url="http://localhost:8000/login")
assert response.status_code == 200
assert "登录" in response.text
# 测试关闭注册弹框
def test_close_registration():
# 点击关闭按钮,关闭注册弹框
response = requests.post(url="http://localhost:8000/close-registration")
assert response.status_code == 200
```
需要注意的是,以上代码仅为示例,实际测试用例需要根据具体需求进行编写。
阅读全文