当前位置: 首页 > news >正文

接口测试返回参数的自动化对比!

引言

在现代软件开发过程中,接口测试是验证系统功能正确性和稳定性的核心环节。接口返回参数的对比不仅是确保接口功能实现的手段,也是测试过程中常见且重要的任务。为了提高对比的效率和准确性,我们可以通过自动化手段实现这一过程。本文将介绍接口测试返回参数自动化对比的原理,展示如何使用 Python 和 MySQL 实现对比功能,并提供最佳实践的建议,帮助读者在实际项目中应用这些技术。

自动化对比的原理

接口测试返回参数的自动化对比涉及以下几个步骤:

  1. 提取和存储预期结果:在测试用例设计阶段,定义接口调用的预期返回结果,并将其存储在文件或数据库中。

  2. 执行接口调用:通过自动化测试脚本执行接口调用,获取实际的返回结果。

  3. 结果比对:将实际返回的结果与预期结果进行比对,以确认接口是否满足需求。

自动化对比的工作流程

  1. 准备测试数据:包括请求参数和预期返回结果。

  2. 执行测试用例:发送请求并接收响应。

  3. 对比实际返回的结果和预期结果:通过编写自动化脚本对比返回的参数。

  4. 记录对比结果:将对比结果存储到数据库中,并生成测试报告。

  5. 数据清理:在测试完成后对数据进行清理以维护数据库的健康状态。

数据库设计与实现

为了实现自动化对比,我们选择 MySQL 数据库来存储测试执行信息和对比结果。以下是数据库设计方案及其实现细节。

数据库表设计

1. 测试执行表

存储每次测试执行的信息。

CREATE TABLE test_execution (execution_id VARCHAR(36) PRIMARY KEY,test_case_id INT NOT NULL,execution_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,status VARCHAR(10) NOT NULL
);
2. 接口调用表

存储每次接口调用的信息。

CREATE TABLE api_call (call_id VARCHAR(36) PRIMARY KEY,execution_id VARCHAR(36),api_endpoint VARCHAR(255) NOT NULL,request_params JSON,response_data JSON,compare_result VARCHAR(10),FOREIGN KEY (execution_id) REFERENCES test_execution(execution_id)
);
3. 对比结果表

存储每次对比的详细信息。

CREATE TABLE compare_result (compare_id VARCHAR(36) PRIMARY KEY,call_id VARCHAR(36),field_name VARCHAR(255),expected_value TEXT,actual_value TEXT,match_result VARCHAR(10),FOREIGN KEY (call_id) REFERENCES api_call(call_id)
);

数据库操作示例

以下是插入测试执行记录、接口调用记录和对比结果记录的代码示例:

import mysql.connector
from uuid import uuid4# 数据库上下文管理器
class Database:def __enter__(self):self.connection = mysql.connector.connect(host="localhost",user="root",password="password",database="test_db")self.cursor = self.connection.cursor()return selfdef __exit__(self, exc_type, exc_value, traceback):self.connection.commit()self.cursor.close()self.connection.close()# 插入测试执行记录
def insert_test_execution(test_case_id):execution_id = str(uuid4())with Database() as db:db.cursor.execute("INSERT INTO test_execution (execution_id, test_case_id, status) VALUES (%s, %s, %s)",(execution_id, test_case_id, 'running'))return execution_id# 插入接口调用记录
def insert_api_call(execution_id, api_endpoint, request_params, response_data, compare_result):call_id = str(uuid4())with Database() as db:db.cursor.execute("INSERT INTO api_call (call_id, execution_id, api_endpoint, request_params, response_data, compare_result) VALUES (%s, %s, %s, %s, %s, %s)",(call_id, execution_id, api_endpoint, json.dumps(request_params), json.dumps(response_data), compare_result))return call_id# 插入对比结果记录
def insert_compare_result(call_id, field_name, expected_value, actual_value, match_result):compare_id = str(uuid4())with Database() as db:db.cursor.execute("INSERT INTO compare_result (compare_id, call_id, field_name, expected_value, actual_value, match_result) VALUES (%s, %s, %s, %s, %s, %s)",(compare_id, call_id, field_name, expected_value, actual_value, match_result))

动态参数的过滤

在接口测试中,动态参数(如时间戳、随机数等)可能会导致对比结果的不一致。为了提高对比的准确性,需要在对比过程中对这些动态参数进行过滤。

动态参数过滤的实现

以下是动态参数过滤的示例代码,过滤掉返回结果中的动态字段:

import requests
import json# 动态参数过滤函数
def filter_dynamic_params(response_data: dict, exclude_keys: list) -> dict:"""过滤动态参数,排除不需要对比的字段。"""return {k: v for k, v in response_data.items() if k not in exclude_keys}# 动态参数列表
dynamic_params = ['timestamp', 'token', 'session_id']# 执行接口调用并对比结果
def call_and_compare(execution_id, url, params, expected_response):response = requests.post(url, json=params)actual_response = response.json()filtered_response = filter_dynamic_params(actual_response, dynamic_params)filtered_expected_response = filter_dynamic_params(expected_response, dynamic_params)compare_result = 'match' if filtered_expected_response == filtered_response else 'mismatch'call_id = insert_api_call(execution_id, url, params, filtered_response, compare_result)for key in filtered_expected_response:if key in filtered_response:match_result = 'match' if filtered_expected_response[key] == filtered_response[key] else 'mismatch'insert_compare_result(call_id, key, filtered_expected_response[key], filtered_response[key], match_result)# 测试用例
def test_interface():execution_id = insert_test_execution(1)url = "http://api.example.com/login"params = {"username": "test", "password": "test"}expected_response = {"status": "success", "timestamp": "ignore"}call_and_compare(execution_id, url, params, expected_response)# 更新测试执行状态with Database() as db:db.cursor.execute("UPDATE test_execution SET status = %s WHERE execution_id = %s", ('completed', execution_id))if __name__ == "__main__":test_interface()

集成对比结果的报告内容

在测试执行完成后,我们可以生成一个对比结果报告,以展示测试的执行情况和结果。

生成报告的代码示例
from jinja2 import Template# 生成对比结果报告
def generate_report(execution_id):with Database() as db:db.cursor.execute("""SELECT a.api_endpoint, a.request_params, a.response_data, a.compare_result, c.field_name, c.expected_value, c.actual_value, c.match_resultFROM api_call aJOIN compare_result c ON a.call_id = c.call_idWHERE a.execution_id = %s""", (execution_id,))results = db.cursor.fetchall()# Jinja2 模板template_str = """<!DOCTYPE html><html><head><title>测试对比结果报告</title></head><body><h1>测试对比结果报告</h1><p><strong>测试执行ID:</strong>{{ execution_id }}</p><table border="1"><tr><th>接口地址</th><th>请求参数</th><th>返回数据</th><th>对比结果</th><th>字段名称</th><th>预期值</th><th>实际值</th><th>匹配结果</th></tr>{% for row in results %}<tr><td>{{ row.api_endpoint }}</td><td>{{ row.request_params }}</td><td>{{ row.response_data }}</td><td>{{ row.compare_result }}</td><td>{{ row.field_name }}</td><td>{{ row.expected_value }}</td><td>{{ row.actual_value }}</td><td>{{ row.match_result }}</td></tr>{% endfor %}</table></body></html>"""template = Template(template_str)html_report = template.render(execution_id=execution_id, results=results)# 保存报告到文件with open(f'report_{execution_id}.html', 'w') as f:f.write(html_report)

对比数据的清理

测试完成后,需要对比数据进行清理,以保持数据库的健康状态。以下是手动和自动数据清理的示例代码。

手动清理数据
def manual_cleanup(execution_id):with Database() as db:db.cursor.execute("DELETE FROM compare_result WHERE call_id IN (SELECT call_id FROM api_call WHERE execution_id = %s)", (execution_id,))db.cursor.execute("DELETE FROM api_call WHERE execution_id = %s", (execution_id,))db.cursor.execute("DELETE FROM test_execution WHERE execution_id = %s", (execution_id,))
自动清理数据
import schedule
import time# 自动清理任务
def auto_cleanup():with Database() as db:db.cursor.execute("""DELETE FROM compare_result WHERE compare_id IN (SELECT compare_id FROM compare_resultWHERE compare_id NOT IN (SELECT compare_id FROM api_call WHERE execution_id IN (SELECT execution_id FROM test_execution WHERE status = 'completed')))""")db.cursor.execute("""DELETE FROM api_call WHERE call_id IN (SELECT call_id FROM api_callWHERE call_id NOT IN (SELECT call_id FROM compare_result))""")db.cursor.execute("""DELETE FROM test_execution WHERE status = 'completed' AND execution_id NOT IN (SELECT execution_id FROM api_call)""")# 设置定时任务
schedule.every().day.at("00:00").do(auto_cleanup)while True:schedule.run_pending()time.sleep(1)

结论

在本文中,我们详细探讨了接口测试返回参数的自动化对比方法,包括动态参数的过滤、对比结果的报告生成以及对比数据的清理。通过合理的工具和技术手段,可以显著提高接口测试的效率和效果。希望本文提供的实践经验和代码示例能帮助从事接口测试的技术人员在实际项目中更好地应用自动化对比技术,提高测试的质量和效率。

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

在这里插入图片描述

http://www.lryc.cn/news/400892.html

相关文章:

  • React基础学习-Day02
  • 切换网页visibilitychange,的升级版实现
  • 基于pytesseract的OCR图片识别
  • Docker_指令篇
  • HAL_UART_Transmit()函数用法
  • OpenCV一个简单的摄像头调用与关闭
  • 深度学习5 神经网络
  • js中! 、!!、?.、??、??=的用法及使用场景
  • 嵌入式面试高频八股文面试题及参考答案
  • 前端练习小项目——方向感应名片
  • 【Vim】为什么程序员喜欢用 Vim
  • stm32h743 NetXduo 实现http server CubeIDE+CubeMX
  • ubuntu服务器部署vue springboot前后端分离项目
  • 【python】pandas报错:UnicodeDecodeError详细分析,解决方案以及如何避免
  • FlinkModule加载HiveModule异常
  • 计算机硬件---如何更新自己电脑的BLOS
  • AI算法17-贝叶斯岭回归算法Bayesian Ridge Regression | BRR
  • 唯众物联网综合实训台 物联网实验室建设方案
  • 深入浅出 Vue.js:从基础到进阶的全面总结
  • 路网双线合并单线——ArcGISpro 解决方法
  • 邮箱验证码功能开发
  • 2024-07-15 Unity插件 Odin Inspector3 —— Button Attributes
  • 根据脚手架archetype快速构建spring boot/cloud项目
  • 安灯系统在电力设备制造业中的应用效果
  • 代码随想录打卡第二十五天
  • openharmony上传图片,并获取返回路径
  • git常用命令及git分支
  • c# 依赖注入-服务的生命周期
  • 一站式短视频矩阵开发,高效托管!
  • 实践致知第16享:设置Word中某一页横着的效果及操作