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

pytest-bdd 行为驱动自动化测试

引言

pytest-bdd 是一个专为Python设计的行为驱动开发(BDD)测试框架,它允许开发人员使用自然语言(如Gherkin)来编写测试用例,从而使测试用例更易于理解和维护。

安装

通过pip安装

pip install pytest-bdd

介绍

特性文件(Feature File):定义了要测试的系统功能。通常以.feature为扩展名,并使用Gherkin语言编写。特性文件包含特性名称、描述以及一个或多个场景。
场景文件(Scenario File):定义了特定场景的步骤。虽然pytest-bdd不直接要求场景文件具有特定的扩展名或格式,但通常会将场景步骤的定义放在Python测试文件中,并使用pytest-bdd提供的装饰器来映射场景文件中的步骤。
步骤定义:将特性文件中的步骤映射到Python代码中的函数。这些函数使用pytest-bdd提供的@given、@when、@then等装饰器来定义。
钩子:在特定测试事件(如测试开始或结束)时调用的函数。pytest-bdd提供了几个内置钩子,如before_scenario、after_scenario、before_step、after_step等,允许在测试的不同阶段执行自定义代码。

简单代码实现

feature文件编写

Feature: TestApiScenario: Successful TestGiven: 我是接口测试When: 调用 "www.baidu.com" 接口When: 使用 "get" 请求Then: 调用成功

python文件编写

import pytest
from pytest_bdd import scenarios, given, when, then, parsers
import requestsclass ApiCaller:def __int__(self):self.api = Noneself.methods = Noneself.types = Noneself.params = Nonedef call_api(self):print(self.api)result = requests.request(method,'http://' + self.api)return resultscenarios('test.feature')@pytest.fixture
@given('我是测试接口')
def api_caller():return ApiCaller()@when(parsers.parse('调用 "{api}" 接口'))
def api(api_caller, api):api_caller.api = api@when(parsers.parse('使用 "{methods}" 请求'))
def method(api_caller, methods):api_caller.methods = methods@then('调用成功')
def asserts(api_caller):result = api_caller.call_api()assert result.status_code == 200

运行

pytest test.py

拓展

通过上面简单的使用,我们知道行为驱动测试是通过pytest-bdd提供的@given、@when、@then等装饰器,来获取到feature文件中的参数,然后来进行编码测试。所以我们可以编写一个较为通用的python测试脚本,来运行所有的feature文件。这样我们只需要通过写feature文件,就可以进行接口自动化测试了。

import pytest
from pytest_bdd import scenarios, given, when, then, parsers
import requestsclass ApiCaller:def __int__(self):self.api = Noneself.methods = Noneself.types = Noneself.params = Noneself.headers = Nonedef call_api(self):result = requests.request(self.methods,'http://' + self.api)return resultscenarios('test1.feature')@pytest.fixture
@given('我是测试接口')
def api_caller():return ApiCaller()@when(parsers.parse('调用 "{api}" 接口'))
def api(api_caller, api):api_caller.api = api@when(parsers.parse('使用 "{methods}" 请求'))
def method(api_caller, methods):api_caller.methods = methods@when(parsers.parse('参数类型 "{types}"'))
def types(api_caller, types):api_caller.types = types@when(parsers.parse('参数 "{params}"'))
def params(api_caller, params):api_caller.params = params@when(parsers.parse('参数为空'))
def params_none():pass@when(parsers.parse('请求头 "{headers}"'))
def headers(api_caller, headers):api_caller.headers = headers@when(parsers.parse('请求头为空'))
def params_none():pass@then('调用成功')
def asserts(api_caller):result = api_caller.call_api()assert result.status_code == 200

我们把一些特殊场景提前定义好,比如请求头为空,参数为空。也可以根据自身业务来继续添加一些特殊场景,比如不同的校验方式等。这样,我们就可以通过一个python脚本,来实现不同的feature文件进行接口自动化测试。

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

相关文章:

  • PostgreSQL11 | 触发器
  • cesium canvas广告牌
  • 使用Floyd算法求解两点间最短距离
  • linux“how_paras.sh“ E212: 无法打开并写入文件
  • CSS mask-image 实现边缘淡出过渡效果
  • 电子元器件—电容和电感(一篇文章搞懂电路中的电容和电感)(笔记)(面试考试必备知识点)电容和电感作用、用途、使用、注意事项、特点等(面试必备)-笔记(详解)
  • 2024HDU Contest 5 Problem 5
  • nGQL入门
  • [CP_AUTOSAR]_系统服务_DEM模块(二)功能规范介绍
  • Linux中yum、rpm、apt-get、wget的区别,yum、rpm、apt-get常用命令,CentOS、Ubuntu中安装wget
  • IPython的使用技巧2
  • win10打开程序闪退的解决方法,亲测好用
  • 木舟0基础学习Java的第二十一天(数据库,MySQL,SQLyog)
  • python-鼠标绘画线条程序
  • 【Python实战】如何优雅地实现 PDF 去水印?
  • Keysight(原Agilent) E4980AL 精密 LCR 表特性与技术指标
  • 【运维】Redis主从复制 配置
  • C++ 微积分 - 求导 - 自动微分(Automatic Differentiation)
  • 面试题-每日5道
  • STM32卡死、跑飞如何调试确定问题
  • 代理模式和Spring MVC
  • 深入理解Vue slot的原理
  • git fetch作用与用法
  • pycharm如何查看git历史版本变更信息
  • 【2.2 python中的变量】
  • Python软体中找出一组字符串的最长公共前缀:算法与实现
  • git lfs使用(huggingface下载大模型文件)-教程记录
  • 1. 什么是操作系统
  • 数据科学 - 数据预处理 (数据清洗,结构化数据)
  • 基于SpringBoot+Vue的校车调度管理系统(带1w+文档)