通过pytest-yaml-allure实现UI和接口数据驱动自动化
一、引言
在软件测试领域,数据驱动测试是一种非常重要的测试策略,它允许我们利用外部数据源(如YAML文件)来定义测试用例,从而极大地提高了测试的可维护性和可扩展性。特别是当面对UI和接口测试时,数据驱动的方式能够让我们更加灵活地应对各种测试场景。本文将介绍如何使用pytest-yaml-allure插件来实现UI和接口的数据驱动自动化测试。
二、pytest-yaml-allure插件简介
pytest-yaml-allure是一个强大的pytest插件,它允许我们直接从YAML文件中读取测试用例并执行。同时,结合Allure报告工具,我们可以生成详细、美观的测试报告,方便团队成员查看和分析测试结果。同时实现所有的底层驱动能力统一封装一个抽象类,如:request,uiautomator2,Selenium,ocr,adb,qdb,ssh,opencv,minicap,飞书通知,文本转语音,图片处理,canoe,pcan,doip,uds等
三、环境准备
在开始之前,请确保你的Python环境已经安装了以下库:
pytest
paramiko
uiautomator2
opencv-python
numpy
scikit-image
scp
allure-pytest
pytest-assume
requests
pyserial
ping3
urllib3==2.0.7
PyYAML
pydub==0.25.1
pyttsx3
Selenium
udsoncan
doipclient
can
四、编写YAML测试用例
在YAML文件中,我们可以定义多个测试用例,每个测试用例包含输入数据和预期结果。以下是一个简单的示例:
# test_cases.yaml
- case_id: 001
name: 登录测试
data:
username: test_user
password: test_pass
expected:
status_code: 200
message: 登录成功
- case_id: 002
name: 接口测试
data:
url: http://example.com/api/v1/users
method: GET
expected:
status_code: 200
response_body: contains "users"
五、编写test_xx.py测试用例
#test_xx.py
from uitl import VehicleControlInterface
class TestDome(object):
def setup_class(self):
"""
初始化测试环境
:return: None
"""
cfg_path = ""
service_list=[]
# 初始化接口
self.vehicle_control = VehicleControlInterface()
def teardown_class(self):
"""
清理测试环境
:return: None
"""
def setup_method(self):
"""
初始化测试用例
:return: None
"""
def teardown_method(self):
"""
清理测试用例
:return: None
"""
def test_dome(self):
"""
测试dome
:return: None
"""
self.vehicle_control.ui_click(element={"text":"驾驶"})
持续更新。。。。。