pytest+uiautomation+allure+Excel 数据驱动桌面自动化

pytest+uiautomation+allure Excel 数据驱动桌面自动化

一、安装依赖

bash 复制代码
pip install pytest uiautomation allure-pytest openpyxl

openpyxl 专门读写xlsx 格式Excel

二、调整项目结构

Plain 复制代码
desktop_automation/
├── config/setting.py
├── data/
│   └── case_data.xlsx   # Excel测试数据
├── common/
│   └── read_excel.py    # 封装读取Excel工具类
├── page/
│   ├── base_page.py
│   └── notepad_page.py
├── testcase/test_notepad.py
├── conftest.py
└── pytest.ini

三、Excel 测试数据格式

新建 data/case\_data\.xlsx,工作表名:input\_data

case_name input_content expect_content
普通文本输入 桌面自动化 Excel 数据驱动 桌面自动化 Excel 数据驱动
特殊字符输入 123@#$ 测试 123@#$ 测试
空值输入
长文本输入 基于 pytest+uiautomation 做桌面 UI 自动化 基于 pytest+uiautomation 做桌面 UI 自动化

四、封装 Excel 读取工具 common/read\_excel.py(_excel.py)

python 复制代码
from openpyxl import load_workbook

class ReadExcel:
    @staticmethod
    def get_excel_data(file_path, sheet_name):
        """
        读取Excel用例数据,返回列表字典格式
        :param file_path: excel路径
        :param sheet_name: 工作表名
        :return: [{},{},...]
        """
        wb = load_workbook(filename=file_path)
        ws = wb[sheet_name]
        # 获取表头
        headers = [cell.value for cell in ws[1]]
        data_list = []
        # 遍历行数据,跳过表头
        for row in ws.iter_rows(min_row=2, values_only=True):
            row_dict = dict(zip(headers, row))
            data_list.append(row_dict)
        wb.close()
        return data_list

五、配置文件 config/setting.py(setting.py)

python 复制代码
# 被测应用
APP_PATH = "notepad.exe"
# 全局等待
IMPLICIT_WAIT = 3
# Excel数据路径
EXCEL_DATA_PATH = "./data/case_data.xlsx"
# Excel工作表名
EXCEL_SHEET = "input_data"

六、页面层代码不变

base\_page.py(_page.py) 不变

notepad\_page.py(_page.py) 不变

七、Excel 数据驱动测试用例 testcase/test\_notepad.py(_notepad.py)

python 复制代码
import pytest
from common.read_excel import ReadExcel
from page.notepad_page import NotePadPage
from config.setting import APP_PATH, EXCEL_DATA_PATH, EXCEL_SHEET

# 读取Excel所有测试数据
def get_excel_case_data():
    return ReadExcel.get_excel_data(EXCEL_DATA_PATH, EXCEL_SHEET)

class TestNotePadExcelDDT:
    def setup_class(self):
        self.page = NotePadPage()
        self.page.start_app(APP_PATH)

    def teardown_class(self):
        self.page.close_app()

    # Excel数据驱动参数化
    @pytest.mark.parametrize("case", get_excel_case_data(), ids=lambda x: x["case_name"])
    def test_input_by_excel(self, case):
        """Excel数据驱动-记事本输入测试"""
        in_text = case["input_content"]
        exp_text = case["expect_content"]

        self.page.input_edit_content(in_text)
        actual = self.page.get_edit_content()
        assert actual == exp_text, f"失败:预期{exp_text},实际{actual}"

八、conftest.py(conftest.py) 失败截图不变

python 复制代码
import pytest
import allure
import uiautomation as auto

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    if report.when == "call" and report.failed:
        try:
            img = auto.CaptureToImage()
            allure.attach(img, "失败截图", allure.attachment_type.PNG)
        except:
            pass

九、pytest.ini 不变

ini 复制代码
[pytest]
testpaths = testcase
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts = -vs --alluredir=./report/temp --clean-alluredir

十、运行命令

bash 复制代码
# 执行用例
pytest

# 生成并打开allure报告
allure generate ./report/temp -o ./report/html --clean
allure open ./report/html

十一、扩展用法

  1. 增加用例状态控制
    Excel 新增列 is\_run,值为1执行,0跳过
    读取数据时过滤:
python 复制代码
data_list = [d for d in data_list if d.get("is_run") == 1]
  1. 区分正向 / 逆向用例

    Excel 加case\_type字段,用例内做不同业务逻辑

  2. 登录场景直接套用

    Excel 填账号、密码、预期结果,页面写登录方法即可

十二、注意事项

  1. 只支持 .xlsx,不支持老版.xls

  2. Excel 表头名称必须和代码取值字段完全一致

  3. 空单元格读取为None,代码可自行转为空字符串

相关推荐
iPad协议个微协议10 小时前
微信自动回复从关键词到 AI:WechatApi 如何连接消息、模型和人工
微信·自动化·微信开发·wechatapi·个人微信号二次开发
babe小鑫10 小时前
信息与计算科学专业应届生面试 怎么证明自己能解决业务问题
学习·r语言·excel
liulilittle10 小时前
mock 规范总纲
ai·自动化·llm·mock·测试·tools
liulilittle12 小时前
麻将结算全链路语义(SETTLE_SEMANTICS)
ai·自动化·llm·mock·lua·测试
jingli913 小时前
恶意退款怎么办、仅退款怎么申诉:六类恶意退款识别特征+取证证据链+反制全流程(商家实操版)
人工智能·自动化·用户运营
苍苍竹林寺13 小时前
Module Builder——Gem200之Alarm模块
自动化·半导体·eap
jingli914 小时前
微信自动回复怎么设置?重复问题怎么自动回答——私域FAQ自动应答搭建全流程(关键词匹配+知识库+语义AI,2026实操版)
人工智能·自动化·用户运营
微信ipad协议开发14 小时前
教培机构微信自动化:从招生引流到社群运维的完整链路
微信·机器人·自动化·教育电商
liulilittle14 小时前
麻将服务器崩溃补偿与异常处理语义(CRASH_SEMANTICS)
ai·自动化·llm·mock·lua·测试·tools
NailiConveyor14 小时前
输送线方案设计的工程基础|布局与接口的技术考量|控制系统架构要点
大数据·自动化·制造·业界资讯