1.测试框架搭建与封装
1.能够对IHRM系统中的登录接口实现接口对象的封装
2.能够对IHRM系统中的员工管理接口实现接口对象的封装
3.能够对IHRM系统中的登录接口实现测试脚本的编写(接口对象调用、断言)
4.能够对IHRM系统中的员工管理接口实现测试脚本的编写(接口对象调用、断言)
1.1 搭建接口测试框架
接口测试框架目录如下所示:
IHRMPro
api
data
report
scripts
tools
app.py
run_suite.py
utils.py
1.2 封装被测接口
(1).登录接口封装
import requests
# 创建接口类
class LoginAPI:
# 初始化
def __init__(self):
self.url = "http://localhost:8088..." # 定义接口调用方法
def login(self, login_data):
return requests.post(url=self.url, json=login_data)
(2).员工管理接口封装
# 创建接口类
class EmployeeAPI:
# 初始化
def __init__(self):
self.url_add_employee = app.BASE_URL + "/api/sys/user"
self.url_update_employee = app.BASE_URL + "/api/sys/user/{}"
self.url_get_employee = app.BASE_URL + "/api/sys/user/{}"
self.url_delete_employee = app.BASE_URL + "/api/sys/user/{}"
# 员工添加
def add_employee(self, add_employee_data):
return requests.post(url=self.url_add_employee, json=add_employee_data, headers=app.headers_data)
# 员工修改
def update_employee(self, employee_id, update_data):
pass
# 员工查询
def get_employee(self, employee_id):
pass
# 员工删除
def delete_employee(self, employee_id):
pass
2.定义登录接口测试用例
2.1 登录
(1).登录成功
# 创建测试类
class TestLogin(unittest.TestCase): # 前置处理
def setUp(self):
self.login_api = LoginAPI() # # 后置处理
# 定义测试用例
# case001 登录成功
def test01_case001(self):
# 调用登录接口进行登录
response = self.login_api.login({"mobile":"13800000002", "password":"123456"})
print(response.json())
# 断言
self.assertEqual(200, response.status_code)
self.assertEqual(True, response.json().get("success"))
self.assertEqual(10000, response.json().get("code"))
self.assertIn("操作成功", response.json().get("message"))
(2).其他用例实现
class TestLogin(unittest.TestCase): # 前置处理
def setUp(self):
self.login_api = LoginAPI() # # 后置处理
# case002 不输入手机号
def test02_case002(self):
# 调用登录接口进行登录
response = self.login_api.login({"mobile": "", "password": "123456"}) print(response.json())
# 断言
self.assertEqual(200, response.status_code)
self.assertEqual(False, response.json().get("success"))
self.assertEqual(20001, response.json().get("code"))
self.assertIn("用户名或密码错误", response.json().get("message"))
3.生成测试报告
3.1 生成测试报告
# 组装测试套件
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestLogin))
# 指定测试报告的路径
report = "./report/report-{}.html".format(time.strftime("%Y%m%d-%H%M%S")) # 打开文件流
with open(report, "wb") as f:
# 创建HTMLTestRunner运行器
runner = HTMLTestRunner(f, title="API Report")
# 执行测试套件
runner.run(suite)
4.参数化
4.1 构造数据文件
创建json数据文件
[
{
"desc": "case001 登录成功",
"login_data": {
"mobile": "13800000002",
"password": "123456"
},
"status_code": 200,
"success": true,
"code": 10000,
"message": "操作成功"
},
{
"desc": "case002 不输入手机号",
"login_data": {
"mobile": "",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case003 不输入密码",
"login_data": {
"mobile": "13800000002",
"password": ""
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case004 手机长度小于11位",
"login_data": {
"mobile": "1380000000",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case005 手机场地大于11位",
"login_data": {
"mobile": "138000000023",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case006 手机号输入非数字",
"login_data": {
"mobile": "error",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case007 输入未注册手机号",
"login_data": {
"mobile": "13812321236",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case008 多参",
"login_data": {
"mobile": "13800000002",
"password": "123456",
"haha": "xixi"
},
"status_code": 200,
"success": true,
"code": 10000,
"message": "操作成功"
},
{
"desc": "case009 少参-缺少手机号",
"login_data": {
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case010 少参-缺少密码",
"login_data": {
"mobile": "13800000002"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case011 无参", "status_code": 200,
"success": false,
"code": 99999,
"message": "抱歉,系统繁忙"
},
{
"desc": "case012 错误参数-手机号",
"login_data": {
"mobiel": "13800000002",
"password": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
},
{
"desc": "case013 错误参数-密码",
"login_data": {
"mobile": "13800000002",
"passwd": "123456"
},
"status_code": 200,
"success": false,
"code": 20001,
"message": "用户名或密码错误"
}
]
4.2 改写测试用例脚本
# 导包
import json
import unittest
from api.login import LoginAPI
from parameterized import parameterized
# 构建测试数据
def build_data():
# 指定文件路径
json_file = "../data/login.json"
# 打开json文件
test_data = []
with open(json_file, encoding="utf-8") as f:
# 数据处理过程
pass
return test_data
# 创建测试类
class TestLogin(unittest.TestCase):
# 定义测试用例: 实现参数化
@parameterized.expand(build_data)
def test01_login(self, login_data, status_code, success, code, message):
pass