文章目录
一、登录模块
人力资源管理系统,前面登录模块中,测试用例写了15条,这里面选取前三条测试用例进行编写。

1.1 登录成功
import requests
# 定义测试类
class TestIhrmLogin(object):
# 定义方法 - 登录成功
def test01_login_success(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
# headers={"Content-Type": "application/json"},
json={"mobile": "13800000002", "password": "123456"})
# 打印响应结果,转json格式
print("登录成功:", resp.json())
# 断言
assert 200 == resp.status_code
assert True == resp.json().get("success")
assert 10000 == resp.json().get("code")
assert "操作成功" in resp.json().get("message")
1.2 手机号不存在(未注册)
# 定义方法 - 手机号不存在(未注册)
def test02_tel_not_exists(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
json={"mobile": "13804789371", "password": "123456"})
# 打印响应结果,转json格式
print("手机号不存在:", resp.json())
# 断言
assert 200 == resp.status_code
assert False == resp.json().get("success")
assert 20001 == resp.json().get("code")
assert "用户名或密码错误" in resp.json().get("message")
1.3 密码错误
# 定义方法 - 密码错
def test03_pwd_err(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
json={"mobile": "13800000002", "password": "123456890"})
# 打印响应结果,转json格式
print("密码错:", resp.json())
# 断言
assert 200 == resp.status_code
assert False == resp.json().get("success")
assert 20001 == resp.json().get("code")
assert "用户名或密码错误" in resp.json().get("message")
二、完整代码
2.1 pytest_test_login.py
import requests
# 定义测试类
class TestIhrmLogin(object):
# 定义方法 - 登录成功
def test01_login_success(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
# headers={"Content-Type": "application/json"},
json={"mobile": "13800000002", "password": "123456"})
# 打印响应结果,转json格式
print("登录成功:", resp.json())
# 断言
assert 200 == resp.status_code
assert True == resp.json().get("success")
assert 10000 == resp.json().get("code")
assert "操作成功" in resp.json().get("message")
# 定义方法 - 手机号不存在(未注册)
def test02_tel_not_exists(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
json={"mobile": "13804789371", "password": "123456"})
# 打印响应结果,转json格式
print("手机号不存在:", resp.json())
# 断言
assert 200 == resp.status_code
assert False == resp.json().get("success")
assert 20001 == resp.json().get("code")
assert "用户名或密码错误" in resp.json().get("message")
# 定义方法 - 密码错
def test03_pwd_err(self):
# 发送 post请求,登录 ihrm。得响应结果
resp = requests.post(url="http://ihrm-test.itheima.net/api/sys/login",
json={"mobile": "13800000002", "password": "123456890"})
# 打印响应结果,转json格式
print("密码错:", resp.json())
# 断言
assert 200 == resp.status_code
assert False == resp.json().get("success")
assert 20001 == resp.json().get("code")
assert "用户名或密码错误" in resp.json().get("message")
2.2 pytest.ini文件
[pytest]
addopts = -s --html=./report/ihrm_report.html --self-contained-html
testpaths = ./
python_files = pytest*.py
python_classes = Test*
python_functions = test*
2.3 终端运行
C:\XM1>pytest

三、小结


被折叠的 条评论
为什么被折叠?



