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

引言

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

安装

通过pip安装

shell 复制代码
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文件编写

yaml 复制代码
Feature: TestApi

	Scenario: Successful Test
		Given: 我是接口测试
		When: 调用 "www.baidu.com" 接口
		When: 使用 "get" 请求
		Then: 调用成功

python文件编写

python 复制代码
import pytest
from pytest_bdd import scenarios, given, when, then, parsers
import requests

class ApiCaller:
    def __int__(self):
        self.api = None
        self.methods = None
        self.types = None
        self.params = None

    def call_api(self):
        print(self.api)
        result = requests.request(method,'http://' + self.api)
        return result

scenarios('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

运行

shell 复制代码
pytest test.py

拓展

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

python 复制代码
import pytest
from pytest_bdd import scenarios, given, when, then, parsers
import requests

class ApiCaller:
   def __int__(self):
       self.api = None
       self.methods = None
       self.types = None
       self.params = None
       self.headers = None

   def call_api(self):
       result = requests.request(self.methods,'http://' + self.api)
       return result

scenarios('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文件进行接口自动化测试。

相关推荐
waterHBO1 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate4 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio6 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal7 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
artificiali8 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
RaidenQ8 小时前
2024.9.13 Python与图像处理新国大EE5731课程大作业,索贝尔算子计算边缘,高斯核模糊边缘,Haar小波计算边缘
图像处理·python·算法·课程设计
花生了什么树~.8 小时前
python基础知识(六)--字典遍历、公共运算符、公共方法、函数、变量分类、参数分类、拆包、引用
开发语言·python
Trouvaille ~9 小时前
【Python篇】深度探索NumPy(下篇):从科学计算到机器学习的高效实战技巧
图像处理·python·机器学习·numpy·信号处理·时间序列分析·科学计算