《一》项目框架
(1)封装数据base文件夹:封装好的请求(如:post、get请求)
(2)存放数据data文件夹:将数据写在excel文件中进行存放
(3)读取数据ib文件夹:进行读取数据
(4)测试数据test_case文件夹:测试用例(测试过程中,数据可能需要在数据库中断言)
(5)日志文件夹:程序出现问题记录在日志文件夹中
(6)生成测试报告文件夹:测试报告的文件夹
(7)数据库封装文件夹:可以放在base文件夹或ib文件夹
(8)邮件文件夹
《二》excel单接口测试&&excel实现接口关联
(1)项目文件层级目录
[base文件夹] 封装请求
base/httpclient.py
[data文件夹] 存放数据
data/login1.xlsx
data/process.xlsx
[lib文件夹] 读取数据
lib/convert.py
lib/excelRead.py
[test_case文件夹] 测试用例
test_case/login_test.py
test_case/process_test.py
main.py
run.py
(2)代码实现流程
1.新建base文件夹,将httpclient.py封装好的数据粘贴到base文件夹
2.新建data文件夹,将login.xlsx写好的数据粘贴到data文件夹
--login.xlsx格式: id、url、data、method、param_type、expect、sjmsg
3.新建lib文件夹,新建excelRead.py文件
--加载工作薄,获取excel测试数据
4.新建test_case文件夹,新建login_test.py文件(可以新建一个存放路径的管理文件的py)
--发送请求,断言将结果写到excel里面去
5.根目录下新建run.py文件
--生成测试报告
--base/httpclient.py: @allure.step('发送{method}请求')
--test_case/login_test.py: 以用例中的模块作为测试报告的模块名(feature、story、title、description、severity)
(1-5):实现了《一》excel单接口测试
6.将data/process.xlsx写好的数据粘贴到data文件夹
--process.xlsx格式: id、url、data、headers(token)、method、param_type、expect、sjmsg(登录>获取个人信息>选择商品>添加购物车)
7.在test_case文件夹下,新建process_test.py文件
--运行文件:测码学院VIP课程接口swagger文档 (登录>获取个人信息>选择商品>添加购物车)
--登录: 正常运行
--获取个人信息:
(1)test_case/process_test.py 根本没有接收字符串的内容(新增:headers=headers)
(2)base/httpclient.py 请求头是字符串类型 需要改成字典类型(新增:headers = eval(headers) if headers is not None else headers)
(3)base/httpclient.py 获取个人信息:后面设置接收字符串的内容(新增:headers=headers),再走魔法方法这里一遍之后,headers就没值了
修改策略:将原先的代码headers=None 需要修改为: headers=headers进行传过去就正常了
(4)base/httpclient.py
a.get请求参数data有的为空,有的拼接再url地址栏上,按地址进行传参,所以删除params=data
b.如果data没有数据,为空,否则拼接在后面。
requestUrl = '%s%s'%(url, "" if data is None else data)
url=url 修改为 url=requestUrl
新增:headers=headers
c.如果请求参数data有数据
response = self.session.request(method=method, url=url, params=data, **kwargs)
新增:headers=headers
--购物: base/httpclient.py elif method=='POST'方法 新增:headers=headers
(3)代码实现内容
base
base/httpclient.py
# 专门封装getpost请求方式
import allure
import requests
class HttpClient:
# 只要用post,get请求方式 就创建会话
def __init__(self):
self.session=requests.Session()
# 发送请求 请求方式,接口地址 ,接口参数类型,接口数据,头部信息,q其他的信息
# 判断 你要发送的是请求方式 如果get post
# post请求参数类型 json data
@allure.step('发送{method}请求')
def send_request(self,method,url,param_type=None,data=None,headers=None,**kwargs):
# 请求方式转成大写
method=method.upper()
# print(method)
param_type=param_type.upper()
# 请求头是字符串类型 需要改成字典类型
headers = eval(headers) if headers is not None else headers
if method=='GET':
# 请求方式为get的接口,请求参数data有的为空,有的拼接再url地址栏上。如下面两个接口
# 1.获取个人信息(http://39.98.138.157:5000/api/getuserinfo)
# 2.选择商品(http://39.98.138.157:5000/api/getproductinfo?productid=8888)
# 3.因为[选择商品]的接口:params_type=url,按地址进行传参,所以删除params=data
if param_type=='url':
# 如果data没有数据,为空,否则拼接在后面
requestUrl = '%s%s'%(url, "" if data is None else data)
# url=url 修改为: url = requestUrl
response=self.session.request(method=method, url=requestUrl, headers=headers, **kwargs)
else:
response = self.session.request(method=method, url=url, headers=headers, params=data, **kwargs)
elif method=='POST':
if param_type=='FORM':
response=self.session.request(method=method, url=url, data=data, headers=headers, **kwargs)
else:
# json类型数据接收参数类型,需要将字符串类型转换成字典类型进行传参
requestData = eval(data)
response=self.session.request(method=method,url=url,json=requestData, headers=headers, **kwargs)
elif method=='DELETE':
if param_type == 'FORM':
response = self.session.request(method=method, url=url, data=data, **kwargs)
else:
response = self.session.request(method=method, url=url, json=data, **kwargs)
elif method=='PUT':
if param_type == 'FORM':
response = self.session.request(method=method, url=url, data=data, **kwargs)
else:
response = self.session.request(method=method, url=url, json=data, **kwargs)
else:
raise ValueError
return response
'''
1.魔法方法 send_request 实例化类.方法
2.实例化类 HttpClient().send_request() a=HttpClient()
3.[test_case文件夹] 测试用例
test_case/login_test.py test_case/process_test.py
这两个类的这句代码: httpclient = HttpClient()的下一步将进入魔法方法里面
4.获取个人信息:后面设置接收字符串的内容(新增:headers=headers),再走魔法方法这里一遍之后,headers就没值了
修改策略:将原先的代码headers=None 需要修改为: headers=headers进行传过去就正常了
'''
def __call__(self, method,url,param_type,data=None,headers=None,**kwargs):
return self.send_request(method,url,param_type,data,headers=headers,**kwargs)
def close_session(self):
self.session.close()
data
data/login1.xlsx
data/process.xlsx
lib
lib/excelRead.py
# data/login.xlsx 有数据的前提下,进行读取excel里面的数据
import openpyxl
class excelData:
def getExcel(self,filename):
# 加载工作薄
workbook = openpyxl.load_workbook(filename)
va2=[]
for name in workbook.sheetnames:
# print(name) # Sheet1
sheet = workbook[name]
# print((sheet)) # <Worksheet "Sheet1">
va=[]
for va in sheet.values:
if type(va[0]) is int:
# print(va)
va2.append(va)
# print(va2)
return va2
# 把excel里面所有的值得都取出来,为了后面方便使用,需要封装起来
# 实例化类().方法()
if __name__ == '__main__':
# a = excelData().getExcel('../data/login1.xlsx')
a = excelData().getExcel('../data/process_test.xlsx')
print(a)
'''
1.已经把excel里面的数据拿到了
2.测试数据,测试登录(正常情况和异常情况)
'''
test_case
test_case/login_test.py
'''
1.测试excel里面的数据
2.知识点:写这块 用哪个知识点进行读取数据做测试
3.测试数据做登录测试,用什么知识点(推荐用单元测试框架,专门管理测试用例:pytest/unittest)
4.用代码去写测试用例,首先要想到: pytest/unittest(除了管理用例,还可以做参数化)
5.从yaml文件中拿到数据,也可以从excel文件中拿到数据,也可以生成测试报告
6.测试登录:登录数据需要拿到
7.拿到登录数据,发送请求,写过一次封装请求
'''
import allure
import openpyxl
import pytest
from test016.base.httpclient import HttpClient
from test016.lib.excelRead import excelData
class TestCase:
@pytest.mark.parametrize('case', excelData().getExcel('./data/login1.xlsx'))
def test_commapi(self, case):
'''
1.发送请求 需要传参 参数从哪里来?excel里面拿即可(如下)
2.我们要的参数已经拿到了,可以做接口测试
'''
# 以用例中的模块作为测试报告的模块名(feature、story、title、description、severity)
if case[7] is not None:
allure.dynamic.feature(case[7])
if case[8] is not None:
allure.dynamic.story(case[8])
if case[9] is not None:
allure.dynamic.title(case[9])
if case[10] is not None:
allure.dynamic.description(case[10])
if case[11] is not None:
allure.dynamic.severity(case[11])
id = case[0] # id
url = case[1] # url
body = case[2] # 参数data
method = case[3] # 请求方式(get post)
param_type = case[4] # 参数类型(json)
expect = case[5] # 预期结果
# print('wwwww', case)
httpclient = HttpClient()
# method, url, param_type = None, data = None, headers = None, ** kwargs
'''
1.发送请求: 如果发送失败,怎么办?(采取策略:打断点调试代码)
2.测试数据获取到了,没有问题,调试点击下一步
'''
common = httpclient(method=method, url=url, param_type=param_type, data=body)
'''
1.打印结果(4个结果都获取到了结果)
2.断言出现问题,需要进行调试或看报错信息
3.根据报错信息,得知:数据类型不一致(字符串和字典)
4.字符串数据类型需要转换成字典类型
5.将结果写到excel里面去
'''
print(common.json())
dict_expect = eval(expect)
try:
assert dict_expect == common.json()
rel = '用例成功'
except Exception as e:
rel = '用例失败'
workbook=openpyxl.load_workbook('./data/login1.xlsx')
sheet = workbook['Sheet1']
# 将数据写入
sheet.cell(id+1, 7).value=rel
workbook.save('./data/login1.xlsx')
if __name__ == '__main__':
pytest.main(['-sv', 'login_test.py'])
pytest.main(['-v', 'login_test.py'])
# 登录测试用例完成了,测试报告:需要生成测试报告
test_case/process_test.py
import allure
import openpyxl
import pytest
from test016.base.httpclient import HttpClient
from test016.lib.excelRead import excelData
class TestCase:
# @pytest.mark.parametrize('case', excelData().getExcel('./data/login1.xlsx'))
@pytest.mark.parametrize('case', excelData().getExcel('../data/process.xlsx'))
def test_commapi(self, case):
'''
1.发送请求 需要传参 参数从哪里来?excel里面拿即可(如下)
2.我们要的参数已经拿到了,可以做接口测试
'''
id = case[0] # id
url = case[1] # url
body = case[2] # 参数data
headers = case[3] # 请求头
method = case[4] # 请求方式(get post)
param_type = case[5] # 参数类型(json)
expect = case[6] # 预期结果
print('wwwww', case)
httpclient = HttpClient()
# method, url, param_type = None, data = None, headers = None, ** kwargs
'''
1.发送请求: 如果发送失败,怎么办?(采取策略:打断点调试代码)
2.测试数据获取到了,没有问题,调试点击下一步
3.获取个人信息问题:请求头是字符串类型,根本没有接收字符串的内容(新增:headers=headers)
'''
common = httpclient(method=method, url=url, param_type=param_type, data=body, headers=headers)
'''
1.打印结果(4个结果都获取到了结果)
2.断言出现问题,需要进行调试或看报错信息
3.根据报错信息,得知:数据类型不一致(字符串和字典)
4.字符串数据类型需要转换成字典类型
5.将结果写到excel里面去
'''
print(common.json())
dict_expect = eval(expect)
try:
assert dict_expect == common.json()
rel = '用例成功'
except Exception as e:
rel = '用例失败'
# workbook=openpyxl.load_workbook('./data/login1.xlsx')
workbook=openpyxl.load_workbook('../data/process.xlsx')
sheet = workbook['Sheet1']
# 将数据写入
sheet.cell(id+1, 8).value=rel
# workbook.save('./data/login1.xlsx')
workbook.save('../data/process.xlsx')
if __name__ == '__main__':
# pytest.main(['-sv', 'login_test.py'])
# pytest.main(['-v', 'login_test.py'])
pytest.main(['-sv', 'process_test.py'])
pytest.main(['-v', 'process_test.py'])
run.py
import os
import pytest
if __name__ == '__main__':
pytest.main(['-sv', './test_case/login_test.py', '--alluredir', 'allure-results', '--clean-alluredir'])
os.system('allure serve allure-results')
人生苦短,及时行乐
分类:
测码教育 / 课后作业
, 测码教育 / 课堂代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现