📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)
📝 职场经验干货:
在接口自动化测试中,数据库断言是一个重要的环节,它能够验证接口操作后的数据库状态是否符合预期。本文将详细介绍如何在 Pytest 框架中封装 MySQL 数据库数据断言,包括参数化、返回数据格式为 JSON 以及日志记录等。
一、封装 MySQL 数据库连接
首先,我们需要封装一个 MySQL 数据库连接工具类,用于执行 SQL 查询和断言。
(一)安装依赖库
pip install pymysql
(二)封装数据库连接类
import pymysql
class MySQLDatabase:
def __init__(self, host, user, password, db):
self.host = host
self.user = user
self.password = password
self.db = db
self.connection = None
def connect(self):
self.connection = pymysql.connect(
host=self.host,
user=self.user,
password=self.password,
db=self.db,
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)
def execute_query(self, query):
ifnotself.connection:
self.connect()
with self.connection.cursor() as cursor:
cursor.execute(query)
result = cursor.fetchall()
return result
def close(self):
ifself.connection:
self.connection.close()
二、封装数据库断言函数
接下来,封装一个数据库断言函数,用于验证数据库查询结果是否符合预期。
def assert_db_query(query, expected_result):
"""
验证数据库查询结果是否符合预期。
:param query: SQL 查询语句
:param expected_result: 预期的查询结果
"""
db = MySQLDatabase(host='localhost', user='root', password='password', db='testdb')
db.connect()
result = db.execute_query(query)
db.close()
assert result == expected_result, f"数据库查询结果不匹配。预期:{expected_result},实际:{result}"
三、参数化测试用例
为了提高测试的灵活性和可维护性,可以使用 Pytest 的 parametrize 功能进行参数化测试。
(一)定义测试数据
test_data = [
("SELECT * FROM users WHERE id=1", [{"id": 1, "name": "John Doe"}]),
("SELECT * FROM users WHERE id=2", [{"id": 2, "name": "Jane Doe"}]),
]
(二)编写参数化测试用例
import pytest
@pytest.mark.parametrize("query, expected_result", test_data)
def test_database(query, expected_result):
assert_db_query(query, expected_result)
四、返回数据格式为 JSON
在实际接口测试中,接口返回的数据通常是 JSON 格式。为了方便取值和断言,可以将数据库查询结果转换为 JSON 格式。
(一)封装 JSON 格式断言函数
import json
def assert_json(response, expected_json):
"""
验证接口返回的 JSON 数据是否符合预期。
:param response: 接口返回的响应对象
:param expected_json: 预期的 JSON 数据
"""
actual_json = response.json()
assert actual_json == expected_json, f"JSON 数据不匹配。预期:{expected_json},实际:{actual_json}"
(二)结合数据库查询结果
def test_api_and_db():
# 发起接口请求
response = requests.get("https://api.example.com/users/1")
assert_json(response, {"id": 1, "name": "John Doe"})
# 验证数据库查询结果
query = "SELECT * FROM users WHERE id=1"
expected_result = [{"id": 1, "name": "John Doe"}]
assert_db_query(query, expected_result)
五、日志记录封装
为了更好地追踪测试过程,可以在测试用例中添加日志记录。可以使用 logging 模块或第三方库 Loguru。
(一)安装 Loguru
pip install loguru
(二)封装日志记录器
from loguru import logger
logger.add("logs/{time}.log", rotation="1 day", compression="zip")
(三)在测试用例中使用日志记录
def test_api_and_db():
logger.info("开始测试接口和数据库")
response = requests.get("https://api.example.com/users/1")
logger.info("接口请求完成")
assert_json(response, {"id": 1, "name": "John Doe"})
query = "SELECT * FROM users WHERE id=1"
expected_result = [{"id": 1, "name": "John Doe"}]
assert_db_query(query, expected_result)
logger.info("数据库验证完成")
六、总结
通过封装 MySQL 数据库连接、断言函数、参数化测试用例、JSON 格式验证以及日志记录,可以显著提高接口自动化测试的效率和可维护性。希望本文能够对读者在接口自动化框架中实现 MySQL 数据库数据断言封装提供帮助。
最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】


6304

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



