pytest + yaml 框架 -57.用例前置和后置操作执行sql

前言

前面一篇已经介绍在用例中可以查询以及在断言中实现sql,本篇讲解在用例的前置和后置操作中执行sql

配置mysql环境,参考前面一篇https://www.cnblogs.com/yoyoketang/p/16977960.html

有 2 内置的函数可以使用

  • query_sql(sql) 查询 sql, 查询无结果返回[], 查询只有一个结果返回 dict, 查询多个结果返回 List[dict]
  • execute_sql(sql) 执行 sql, 操作新增,修改,删除的 sql

按步骤执行sql

方案一:可以对测试用例,加一个步骤,这样就可以实现在接口的请求前或请求后执行sql

请求前执行 sql, 执行sql的函数随便定义一个关键字,比如print,variables,extract 它都会去执行函数

yaml 复制代码
config:
  name: 1.执行sql当成用例的步骤
  variables:
    sql1: update auth_user set email='121@qq.com' where username like 'test';
test_x1:
-
  name: 步骤1-请求前执行sql
  print: '${execute_sql(sql1)}'
-
  name: 步骤2-执行用例
  request:
    url: http://example.com
    method: get

请求后执行 sql

yaml 复制代码
config:
  name: 1.执行sql当成用例的步骤
  variables:
    sql1: update auth_user set email='121@qq.com' where username like 'test';
test_x2:
-
  name: 步骤1-执行用例
  request:
    url: http://example.com
    method: get
-
  name: 步骤2-请求后执行sql
  print: '${execute_sql(sql1)}'

同一个步骤中实现

方案二:如果你不想分2个步骤,在一个用例步骤中也能实现

  • variables 关键字是在请求之前执行的,所以可以在variables中执行sql,实现前置操作
  • extract 关键字是在请求之后执行的,所以可以在extract中执行sql,实现后置操作

使用示例

yaml 复制代码
config:
  name: 1.执行sql当成用例的步骤
  variables:
    sql1: update auth_user set email='121@qq.com' where username like 'test';
    sql2: update auth_user set email='321@qq.com' where username like 'test';
test_x3:
  name: 步骤1-执行用例
  variables:
    x1: '${execute_sql(sql1)}'
  request:
    url: http://example.com
    method: get
  extract:
    x2: '${execute_sql(sql2)}'

自定义fixture实现前置和后置操作

pytest 框架可以用fixture来实现用例的前置和后置操作,于是可以在conftest.py 中自定义fixture来完成

方案三:自定义fixture功能

python 复制代码
# conftest.py
from pytest_yaml_yoyo.db import ConnectMysql
import pytest


@pytest.fixture(scope='session')
def db_instance(environ):
    # 连上数据库,读取环境配置
    db = ConnectMysql(**environ.DB_INFO)
    yield db
    db.close()


@pytest.fixture()
def run_sql(db_instance):
    sql1 = "update auth_user set email='123@qq.com' where username like 'test';"
    res = db_instance.execute_sql(sql1)
    print(f'前置操作: {res}')
    yield
    sql2 = "update auth_user set email='321@qq.com' where username like 'test';"
    res = db_instance.execute_sql(sql2)
    print(f'后置操作: {res}')

yaml 用例部分

yaml 复制代码
config:
  name: 1.执行sql当成用例的步骤
  fixtures: run_sql
  variables:
    sql1: update auth_user set email='121@qq.com' where username like 'test';
test_x2:
  name: 步骤1-执行用例
  request:
    url: http://example.com
    method: get

总结

使用误区:能否用hooks钩子功能实现操作sql?

关于hooks钩子功能介绍参考这篇https://www.cnblogs.com/yoyoketang/p/16938512.html

hooks 功能的目的是请求预处理,一般接口的sign前面,接口参数加密可以用到,还有response的接口返回对象加密或其它预处理。

操作sql是用例的前置和后置操作,跟接口的预处理没啥关系,所以不能在hooks里面执行sql。

其它解决思路

另外一种解决思路,在测试步骤中新增setup_sql 和 teardown_sql 关键字执行sql语句,目前考虑新增关键字会带来学习成本。

在已有的框架上能解决问题,就先不增加额外关键字了。

如果有同学觉得有必要新增这2个关键字,可以在gitee上提issue https://gitee.com/yoyoketang/pytest-yaml-yoyo/issues

相关推荐
yujkss30 分钟前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx99101333 分钟前
小程序开发APP
开发语言·人工智能·python·yolo
飞翔的佩奇1 小时前
【完整源码+数据集+部署教程】二维码与查找模式检测系统源码和数据集:改进yolo11-CSwinTransformer
python·yolo·计算机视觉·数据集·yolo11·二维码与查找模式检测
大霞上仙1 小时前
实现自学习系统,输入excel文件,能学习后进行相应回答
python·学习·excel
Caven771 小时前
【pytorch】reshape的使用
pytorch·python
无规则ai1 小时前
动手学深度学习(pytorch版):第四章节—多层感知机(5)权重衰减
人工智能·pytorch·python·深度学习
你知道网上冲浪吗2 小时前
【原创理论】Stochastic Coupled Dyadic System (SCDS):一个用于两性关系动力学建模的随机耦合系统框架
python·算法·数学建模·数值分析
钢铁男儿2 小时前
Python 正则表达式核心元字符全解析
python
杨荧3 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化
CodeCraft Studio3 小时前
在 Python 中操作 Excel 文件的高效方案 —— Aspose.Cells for Python
python·ui·excel·报表·aspose·aspose.cells