三公机器人

牛牛机器人,三公撑船机器人,微信牛牛机器人

接口测试——pytest框架续集(二)


在上一篇文章中,我们探讨了pytest框架的基础用法,包括用例编写、运行方式和简单断言。本文将继续深入pytest的进阶特性,帮助你构建更高效、更灵活的接口自动化测试框架。

一、Fixture进阶:共享与参数化

Fixture作为pytest的核心特性,不仅能实现测试前置条件的初始化,还支持通过conftest.py文件实现跨文件共享。在测试项目的根目录下创建conftest.py文件,其中定义的Fixture无需导入,就能被所有测试文件自动识别。例如,我们可以在conftest.py中定义全局的登录Fixture:

import pytest
import requests

@pytest.fixture(scope="session")
def login_token():
   response = requests.post("https://api.example.com/login", data={"username": "test", "password": "123456"})
   return response.json()["token"]

这样,所有测试文件中的测试函数都可以直接使用login_token参数,获取登录凭证。

Fixture还支持参数化,通过params参数可以为Fixture传入多组数据,实现多场景的测试初始化。例如:

import pytest
import requests

@pytest.fixture(params=["user1", "user2"])
def user_token(request):
   username = request.param
   response = requests.post("https://api.example.com/login", data={"username": username, "password": "123456"})
   return response.json()["token"]

测试函数使用该Fixture时,pytest会自动为每组参数生成独立的测试用例。

二、插件生态:扩展测试能力

pytest拥有超过2000个第三方插件,能够满足各种测试需求。以下是几个接口测试中常用的插件:

1. pytest-html:生成美观的测试报告

安装命令:pip install pytest-html运行测试时,通过--html=report.html参数生成HTML格式的测试报告,报告中包含测试结果统计、失败用例详情和执行时间等信息,便于团队协作和问题定位。

2. allure-pytest:专业级测试报告

安装命令:pip install allure-pytestAllure报告支持测试步骤展示、截图附件、历史趋势对比等高级功能。运行测试时,通过--alluredir=./allure-results生成测试结果数据,再使用allure serve ./allure-results启动本地服务查看报告。

3. pytest-xdist:分布式测试

安装命令:pip install pytest-xdist当测试用例数量较多时,使用pytest-xdist可以将测试任务分发到多个CPU核心并行执行,大幅缩短测试时间。运行命令:pytest -n auto,其中auto表示自动检测CPU核心数量。

4. pytest-rerunfailures:失败用例重跑

安装命令:pip install pytest-rerunfailures对于网络波动等偶发性失败的用例,可以通过--reruns=3参数设置失败重跑次数,提高测试的稳定性。

三、数据驱动:Excel与YAML的应用

在接口测试中,数据驱动是实现测试用例与测试数据分离的关键。我们可以使用Excel或YAML文件存储测试数据,通过pytest的参数化功能实现数据驱动测试。

1. Excel数据驱动

使用pandas库读取Excel文件中的测试数据:

import pandas as pd
import pytest
import requests

def get_excel_data():
   df = pd.read_excel("test_data.xlsx")
   return list(zip(df["username"], df["password"], df["expected_code"]))

@pytest.mark.parametrize("username, password, expected_code", get_excel_data())
def test_login(username, password, expected_code):
   response = requests.post("https://api.example.com/login", data={"username": username, "password": password})
   assert response.status_code == expected_code

2. YAML数据驱动

使用pyyaml库读取YAML文件:

import yaml
import pytest
import requests

def get_yaml_data():
   with open("test_data.yaml", "r") as f:
       data = yaml.safe_load(f)
   return data["login_cases"]

@pytest.mark.parametrize("case", get_yaml_data())
def test_login(case):
   response = requests.post("https://api.example.com/login", data={"username": case["username"], "password": case["password"]})
   assert response.status_code == case["expected_code"]

四、自定义插件与Hook函数

pytest允许通过Hook函数自定义测试流程。例如,我们可以通过pytest_addoption添加自定义命令行参数,通过pytest_report_teststatus修改测试结果的显示格式。

以下是一个简单的自定义插件示例,用于添加--env参数指定测试环境:

def pytest_addoption(parser):
   parser.addoption("--env", action="store", default="test", help="指定测试环境:test/prod")

@pytest.fixture(scope="session")
def env(request):
   return request.config.getoption("--env")

测试函数可以通过env参数获取当前测试环境,从而加载不同的配置信息。

通过掌握这些进阶特性,你可以构建出更加强大、灵活的接口自动化测试框架,提高测试效率和质量。在下一篇文章中,我们将探讨pytest与CI/CD工具的集成,实现测试自动化流水线。


Powered By Z-BlogPHP 1.7.3

三公机器人,牛牛机器人,三公撑船机器人,微信牛牛机器人