1、接口自动化流程
- 1.需求分析
- 2.挑选需要做自动化测试的功能
- 3.设计测试用例
- 4.搭建自动化测试环境[可选]
- 5.设计自动化测试项目的架构[可选]
- 6.编写代码
- 7.执行测试用例
- 8.生成测试报告并分析结果
2、框架结构
--api -->封装请求
--scripts -->编写测试脚本
--data -->存放测试数据
--utils -->存放工具类
--report -->测试报告生成目录
app.py -->存放常量
--pytest.ini -->pytest配置文件
测试脚本实现:
test_area.py
python
import requests
from api.AreaAPI import AreaAPI
from api.AreaDBAPI import AreaDBAPI
class TestArea:
def setup_method(self, method):
self.session = requests.Session()
self.area_api = AreaAPI(self.session)
def teardown_method(self, method):
self.session.close()
# 测试查询area列表接口
def test_list_area(self):
response = self.area_api.list_area()
print("状态码:",response.status_code)
print("响应体:",response.text)
# 测试新增area接口
def test_add_area(self):
data = {"areaName":"12345","priority":"54321"}
response = self.area_api.add_area(data=data)
print("状态码:",response.status_code)
print("响应体:",response.text)
# 测试修改area接口
def test_modify_area(self):
id = AreaDBAPI.select_id_by_name("12345")
json = {
"areaId":id,
"areaName":"1234",
"priority":"54321"
}
response = self.area_api.modify_area(json=json)
print("状态码:",response.status_code)
print("响应体:",response.text)
# 测试删除area接口
def test_remove_area(self):
id = AreaDBAPI.select_id_by_name("1234")
params = {"areaId":id}
response = self.area_api.remove_area(params=params)
print("状态码:",response.status_code)
print("响应体:",response.text)
API实现:
python
import requests
from app import AREA_URL, BASE_URL
class AreaAPI:
def __init__(self,session):
self.session = session
self.list_area_url = BASE_URL + AREA_URL + "listarea"
self.add_area_url = BASE_URL + AREA_URL + "addarea"
self.modify_area_url = BASE_URL + AREA_URL + "modifyarea"
self.remove_area_url = BASE_URL + AREA_URL + "removearea"
# 1、查询area
def list_area(self):
response = self.session.get(self.list_area_url)
return response
# 2、新增area
def add_area(self,data):
response = self.session.post(self.add_area_url,data=data)
return response
# 3、修改area
def modify_area(self,json):
response = self.session.put(self.modify_area_url,json=json)
return response
# 4、删除area
def remove_area(self,params):
response = self.session.delete(self.remove_area_url,params=params)
return response
python
BASE_URL = "http://127.0.0.1:1234/"
AREA_URL = "area/"
数据库连接工具:
python
"""
实现封装:将pymysql的常见用法实现封装进一个专门工具类
封装的功能:1、获取连接 2、获取游标 3、释放资源
"""
import pymysql
class DBUtils:
# 工具函数1:获取连接
@classmethod
def create_conn(cls):
return pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='api_test',charset='utf8')
# 工具函数2:获取游标
@classmethod
def create_cur(cls,conn):
return conn.cursor()
# 工具函数3:释放资源
@classmethod
def close_res(cls,conn,cur):
if cur:
cur.close()
if conn:
conn.close()
@classmethod
def conn_commit(cls,conn):
conn.commit()